Permalink
Browse files

Fixes Issue #109 - SHA1 hashing code needs to be optimized

  • Loading branch information...
1 parent b9613c3 commit 2fb3eb9d132808972fded3143b28a0e3297d77c5 @mikechambers committed Jun 18, 2009
Showing with 52 additions and 34 deletions.
  1. +52 −34 src/com/adobe/crypto/SHA1.as
@@ -130,6 +130,7 @@ package com.adobe.crypto
var len:int = blocks.length;
var w:Array = new Array( 80 );
+ var temp:int;
// loop over all of the blocks
for ( var i:int = 0; i < len; i += 16 ) {
@@ -142,24 +143,69 @@ package com.adobe.crypto
var e:int = h4;
// 80 steps to process each block
- // TODO: unroll for faster execution, or 4 loops of
- // 20 each to avoid the k and f function calls
- for ( var t:int = 0; t < 80; t++ ) {
+ var t:int;
+ for ( t = 0; t < 20; t++ ) {
if ( t < 16 ) {
// 6.1.a
w[ t ] = blocks[ i + t ];
} else {
// 6.1.b
- w[ t ] = IntUtil.rol( w[ t - 3 ] ^ w[ t - 8 ] ^ w[ t - 14 ] ^ w[ t - 16 ], 1 );
+ temp = w[ t - 3 ] ^ w[ t - 8 ] ^ w[ t - 14 ] ^ w[ t - 16 ];
+ w[ t ] = ( temp << 1 ) | ( temp >>> 31 )
}
+
+ // 6.1.d
+ temp = ( ( a << 5 ) | ( a >>> 27 ) ) + ( ( b & c ) | ( ~b & d ) ) + e + int( w[ t ] ) + 0x5a827999;
+
+ e = d;
+ d = c;
+ c = ( b << 30 ) | ( b >>> 2 );
+ b = a;
+ a = temp;
+ }
+ for ( ; t < 40; t++ )
+ {
+ // 6.1.b
+ temp = w[ t - 3 ] ^ w[ t - 8 ] ^ w[ t - 14 ] ^ w[ t - 16 ];
+ w[ t ] = ( temp << 1 ) | ( temp >>> 31 )
+
+ // 6.1.d
+ temp = ( ( a << 5 ) | ( a >>> 27 ) ) + ( b ^ c ^ d ) + e + int( w[ t ] ) + 0x6ed9eba1;
+
+ e = d;
+ d = c;
+ c = ( b << 30 ) | ( b >>> 2 );
+ b = a;
+ a = temp;
+ }
+ for ( ; t < 60; t++ )
+ {
+ // 6.1.b
+ temp = w[ t - 3 ] ^ w[ t - 8 ] ^ w[ t - 14 ] ^ w[ t - 16 ];
+ w[ t ] = ( temp << 1 ) | ( temp >>> 31 )
// 6.1.d
- var temp:int = IntUtil.rol( a, 5 ) + f( t, b, c, d ) + e + int( w[ t ] ) + k( t );
+ temp = ( ( a << 5 ) | ( a >>> 27 ) ) + ( ( b & c ) | ( b & d ) | ( c & d ) ) + e + int( w[ t ] ) + 0x8f1bbcdc;
e = d;
d = c;
- c = IntUtil.rol( b, 30 );
+ c = ( b << 30 ) | ( b >>> 2 );
+ b = a;
+ a = temp;
+ }
+ for ( ; t < 80; t++ )
+ {
+ // 6.1.b
+ temp = w[ t - 3 ] ^ w[ t - 8 ] ^ w[ t - 14 ] ^ w[ t - 16 ];
+ w[ t ] = ( temp << 1 ) | ( temp >>> 31 )
+
+ // 6.1.d
+ temp = ( ( a << 5 ) | ( a >>> 27 ) ) + ( b ^ c ^ d ) + e + int( w[ t ] ) + 0xca62c1d6;
+
+ e = d;
+ d = c;
+ c = ( b << 30 ) | ( b >>> 2 );
b = a;
a = temp;
}
@@ -187,34 +233,6 @@ package com.adobe.crypto
}
/**
- * Performs the logical function based on t
- */
- private static function f( t:int, b:int, c:int, d:int ):int {
- if ( t < 20 ) {
- return ( b & c ) | ( ~b & d );
- } else if ( t < 40 ) {
- return b ^ c ^ d;
- } else if ( t < 60 ) {
- return ( b & c ) | ( b & d ) | ( c & d );
- }
- return b ^ c ^ d;
- }
-
- /**
- * Determines the constant value based on t
- */
- private static function k( t:int ):int {
- if ( t < 20 ) {
- return 0x5a827999;
- } else if ( t < 40 ) {
- return 0x6ed9eba1;
- } else if ( t < 60 ) {
- return 0x8f1bbcdc;
- }
- return 0xca62c1d6;
- }
-
- /**
* Converts a ByteArray to a sequence of 16-word blocks
* that we'll do the processing on. Appends padding
* and length in the process.

0 comments on commit 2fb3eb9

Please sign in to comment.