2005-10-16 15:11:50 -04:00
|
|
|
IN: crypto-internals
|
2006-06-15 01:41:58 -04:00
|
|
|
USING: kernel io strings sequences namespaces math parser ;
|
2005-08-25 06:07:50 -04:00
|
|
|
|
2006-02-27 00:04:25 -05:00
|
|
|
: w+ ( int -- int ) + HEX: ffffffff bitand ; inline
|
|
|
|
: nth-int ( string n -- int ) 2 shift dup 4 + rot <slice> le> ; inline
|
|
|
|
: nth-int-be ( string n -- int ) 2 shift dup 4 + rot <slice> be> ; inline
|
|
|
|
: float-sin ( int -- int ) sin abs 4294967296 * >bignum ; inline
|
|
|
|
: update ( num var -- ) [ w+ ] change ; inline
|
2005-08-25 06:07:50 -04:00
|
|
|
|
|
|
|
: update-old-new ( old new -- )
|
2005-09-10 15:53:17 -04:00
|
|
|
[ get >r get r> ] 2keep >r >r w+ dup r> set r> set ; inline
|
2005-08-25 06:07:50 -04:00
|
|
|
|
2006-08-10 00:27:21 -04:00
|
|
|
: calculate-pad-length ( length -- pad-length )
|
|
|
|
dup 56 < 55 119 ? swap - ;
|
2005-08-25 06:07:50 -04:00
|
|
|
|
|
|
|
! pad 0x80 then 00 til 8 bytes left, then 64bit length in bits
|
2006-08-10 00:27:21 -04:00
|
|
|
: preprocess-plaintext ( string big-endian? -- padded-string )
|
2006-09-07 16:15:41 -04:00
|
|
|
>r >sbuf r> over [
|
|
|
|
HEX: 80 ,
|
2006-08-10 00:27:21 -04:00
|
|
|
dup length HEX: 3f bitand calculate-pad-length 0 <string> %
|
2006-09-07 16:15:41 -04:00
|
|
|
length 3 shift 8 rot [ >be ] [ >le ] if %
|
|
|
|
] "" make dupd nappend ;
|
2005-08-25 06:07:50 -04:00
|
|
|
|
2006-02-27 00:04:25 -05:00
|
|
|
: shift-mod ( n s w -- n ) >r shift r> 1 swap shift 1 - bitand ; inline
|
2005-10-16 15:11:50 -04:00
|
|
|
|
|
|
|
IN: crypto
|
|
|
|
: bitroll ( n s w -- n )
|
|
|
|
#! Roll n by s bits to the left, wrapping around after
|
|
|
|
#! w bits.
|
|
|
|
[ 1 - bitand ] keep
|
|
|
|
over 0 < [ [ + ] keep ] when
|
|
|
|
[ shift-mod ] 3keep
|
|
|
|
[ - ] keep shift-mod bitor ; inline
|
|
|
|
|
2006-02-27 00:04:25 -05:00
|
|
|
: hex-string ( str -- str ) [ [ >hex 2 48 pad-left % ] each ] "" make ;
|