ÿþf u n c t i o n   s h a 1 H a s h ( m s g )  
 {  
         / /   c o n s t a n t s   [ § 4 . 2 . 1 ]  
         v a r   K   =   [ 0 x 5 a 8 2 7 9 9 9 ,   0 x 6 e d 9 e b a 1 ,   0 x 8 f 1 b b c d c ,   0 x c a 6 2 c 1 d 6 ] ;  
  
  
         / /   P R E P R O C E S S I N G    
    
         m s g   + =   S t r i n g . f r o m C h a r C o d e ( 0 x 8 0 ) ;   / /   a d d   t r a i l i n g   ' 1 '   b i t   t o   s t r i n g   [ § 5 . 1 . 1 ]  
  
         / /   c o n v e r t   s t r i n g   m s g   i n t o   5 1 2 - b i t / 1 6 - i n t e g e r   b l o c k s   a r r a y s   o f   i n t s   [ § 5 . 2 . 1 ]  
         v a r   l   =   M a t h . c e i l ( m s g . l e n g t h / 4 )   +   2 ;     / /   l o n g   e n o u g h   t o   c o n t a i n   m s g   p l u s   2 - w o r d   l e n g t h  
         v a r   N   =   M a t h . c e i l ( l / 1 6 ) ;                             / /   i n   N   1 6 - i n t   b l o c k s  
         v a r   M   =   n e w   A r r a y ( N ) ;  
         f o r   ( v a r   i = 0 ;   i < N ;   i + + )   {  
                 M [ i ]   =   n e w   A r r a y ( 1 6 ) ;  
                 f o r   ( v a r   j = 0 ;   j < 1 6 ;   j + + )   {     / /   e n c o d e   4   c h a r s   p e r   i n t e g e r ,   b i g - e n d i a n   e n c o d i n g  
                         M [ i ] [ j ]   =   ( m s g . c h a r C o d e A t ( i * 6 4 + j * 4 ) < < 2 4 )   |   ( m s g . c h a r C o d e A t ( i * 6 4 + j * 4 + 1 ) < < 1 6 )   |    
                                             ( m s g . c h a r C o d e A t ( i * 6 4 + j * 4 + 2 ) < < 8 )   |   ( m s g . c h a r C o d e A t ( i * 6 4 + j * 4 + 3 ) ) ;  
                 }  
         }  
         / /   a d d   l e n g t h   ( i n   b i t s )   i n t o   f i n a l   p a i r   o f   3 2 - b i t   i n t e g e r s   ( b i g - e n d i a n )   [ 5 . 1 . 1 ]  
         / /   n o t e :   m o s t   s i g n i f i c a n t   w o r d   w o u l d   b e   ( ( l e n - 1 ) * 8   > > >   3 2 ,   b u t   s i n c e   J S   c o n v e r t s  
         / /   b i t w i s e - o p   a r g s   t o   3 2   b i t s ,   w e   n e e d   t o   s i m u l a t e   t h i s   b y   a r i t h m e t i c   o p e r a t o r s  
         M [ N - 1 ] [ 1 4 ]   =   ( ( m s g . l e n g t h - 1 ) * 8 )   /   M a t h . p o w ( 2 ,   3 2 ) ;   M [ N - 1 ] [ 1 4 ]   =   M a t h . f l o o r ( M [ N - 1 ] [ 1 4 ] )  
         M [ N - 1 ] [ 1 5 ]   =   ( ( m s g . l e n g t h - 1 ) * 8 )   &   0 x f f f f f f f f ;  
  
         / /   s e t   i n i t i a l   h a s h   v a l u e   [ § 5 . 3 . 1 ]  
         v a r   H 0   =   0 x 6 7 4 5 2 3 0 1 ;  
         v a r   H 1   =   0 x e f c d a b 8 9 ;  
         v a r   H 2   =   0 x 9 8 b a d c f e ;  
         v a r   H 3   =   0 x 1 0 3 2 5 4 7 6 ;  
         v a r   H 4   =   0 x c 3 d 2 e 1 f 0 ;  
  
         / /   H A S H   C O M P U T A T I O N   [ § 6 . 1 . 2 ]  
  
         v a r   W   =   n e w   A r r a y ( 8 0 ) ;   v a r   a ,   b ,   c ,   d ,   e ;  
         f o r   ( v a r   i = 0 ;   i < N ;   i + + )   {  
  
                 / /   1   -   p r e p a r e   m e s s a g e   s c h e d u l e   ' W '  
                 f o r   ( v a r   t = 0 ;     t < 1 6 ;   t + + )   W [ t ]   =   M [ i ] [ t ] ;  
                 f o r   ( v a r   t = 1 6 ;   t < 8 0 ;   t + + )   W [ t ]   =   R O T L ( W [ t - 3 ]   ^   W [ t - 8 ]   ^   W [ t - 1 4 ]   ^   W [ t - 1 6 ] ,   1 ) ;  
  
                 / /   2   -   i n i t i a l i s e   f i v e   w o r k i n g   v a r i a b l e s   a ,   b ,   c ,   d ,   e   w i t h   p r e v i o u s   h a s h   v a l u e  
                 a   =   H 0 ;   b   =   H 1 ;   c   =   H 2 ;   d   =   H 3 ;   e   =   H 4 ;  
  
                 / /   3   -   m a i n   l o o p  
                 f o r   ( v a r   t = 0 ;   t < 8 0 ;   t + + )   {  
                         v a r   s   =   M a t h . f l o o r ( t / 2 0 ) ;   / /   s e q   f o r   b l o c k s   o f   ' f '   f u n c t i o n s   a n d   ' K '   c o n s t a n t s  
                         v a r   T   =   ( R O T L ( a , 5 )   +   f ( s , b , c , d )   +   e   +   K [ s ]   +   W [ t ] )   &   0 x f f f f f f f f ;  
                         e   =   d ;  
                         d   =   c ;  
                         c   =   R O T L ( b ,   3 0 ) ;  
                         b   =   a ;  
                         a   =   T ;  
                 }  
  
                 / /   4   -   c o m p u t e   t h e   n e w   i n t e r m e d i a t e   h a s h   v a l u e  
                 H 0   =   ( H 0 + a )   &   0 x f f f f f f f f ;     / /   n o t e   ' a d d i t i o n   m o d u l o   2 ^ 3 2 '  
                 H 1   =   ( H 1 + b )   &   0 x f f f f f f f f ;    
                 H 2   =   ( H 2 + c )   &   0 x f f f f f f f f ;    
                 H 3   =   ( H 3 + d )   &   0 x f f f f f f f f ;    
                 H 4   =   ( H 4 + e )   &   0 x f f f f f f f f ;  
         }  
  
         r e t u r n   H 0 . t o H e x S t r ( )   +   H 1 . t o H e x S t r ( )   +   H 2 . t o H e x S t r ( )   +   H 3 . t o H e x S t r ( )   +   H 4 . t o H e x S t r ( ) ;  
 }  
  
 / /  
 / /   f u n c t i o n   ' f '   [ § 4 . 1 . 1 ]  
 / /  
 f u n c t i o n   f ( s ,   x ,   y ,   z )    
 {  
         s w i t c h   ( s )   {  
         c a s e   0 :   r e t u r n   ( x   &   y )   ^   ( ~ x   &   z ) ;                       / /   C h ( )  
         c a s e   1 :   r e t u r n   x   ^   y   ^   z ;                                         / /   P a r i t y ( )  
         c a s e   2 :   r e t u r n   ( x   &   y )   ^   ( x   &   z )   ^   ( y   &   z ) ;     / /   M a j ( )  
         c a s e   3 :   r e t u r n   x   ^   y   ^   z ;                                         / /   P a r i t y ( )  
         }  
 }  
  
 / /  
 / /   r o t a t e   l e f t   ( c i r c u l a r   l e f t   s h i f t )   v a l u e   x   b y   n   p o s i t i o n s   [ § 3 . 2 . 5 ]  
 / /  
 f u n c t i o n   R O T L ( x ,   n )  
 {  
         r e t u r n   ( x < < n )   |   ( x > > > ( 3 2 - n ) ) ;  
 }  
  
 / /  
 / /   e x t e n d   N u m b e r   c l a s s   w i t h   a   t a i l o r e d   h e x - s t r i n g   m e t h o d    
 / /       ( n o t e   t o S t r i n g ( 1 6 )   i s   i m p l e m e n t a t i o n - d e p e n d a n t ,   a n d    
 / /       i n   I E   r e t u r n s   s i g n e d   n u m b e r s   w h e n   u s e d   o n   f u l l   w o r d s )  
 / /  
 N u m b e r . p r o t o t y p e . t o H e x S t r   =   f u n c t i o n ( )  
 {  
         v a r   s = " " ,   v ;  
         f o r   ( v a r   i = 7 ;   i > = 0 ;   i - - )   {   v   =   ( t h i s > > > ( i * 4 ) )   &   0 x f ;   s   + =   v . t o S t r i n g ( 1 6 ) ;   }  
         r e t u r n   s ;  
 } 
