factor/contrib/crypto/common.factor

63 lines
1.6 KiB
Factor
Raw Permalink Normal View History

2005-10-16 15:11:50 -04:00
IN: crypto-internals
USING: kernel io strings sequences namespaces math prettyprint
2005-12-20 16:39:13 -05:00
test parser lists ;
2005-08-27 15:54:31 -04:00
: w+ ( int -- int )
2005-09-10 15:53:17 -04:00
+ HEX: ffffffff bitand ; inline
: nth-int ( string n -- int )
2005-09-10 15:53:17 -04:00
2 shift dup 4 + rot <slice> le> ; inline
: nth-int-be ( string n -- int )
2005-09-10 15:53:17 -04:00
2 shift dup 4 + rot <slice> be> ; inline
: float-sin ( int -- int )
2005-09-10 15:53:17 -04:00
sin abs 4294967296 * >bignum ; inline
: update ( num var -- )
2005-09-10 15:53:17 -04:00
[ w+ ] change ; inline
: 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
! calculate pad length. leave 8 bytes for length after padding
: zero-pad-length ( length -- pad-length )
2005-09-10 15:53:17 -04:00
dup 56 < 55 119 ? swap - ; ! one less for first byte of padding 0x80
! pad 0x80 then 00 til 8 bytes left, then 64bit length in bits
: pad-string-md5 ( string -- padded-string )
[
2005-09-10 15:53:17 -04:00
dup % HEX: 80 ,
2005-12-29 15:13:57 -05:00
dup length HEX: 3f bitand zero-pad-length 0 <string> %
2005-09-10 15:53:17 -04:00
dup length 3 shift 8 >le %
] "" make nip ;
: pad-string-sha1 ( string -- padded-string )
[
2005-09-10 15:53:17 -04:00
dup % HEX: 80 ,
2005-12-29 15:13:57 -05:00
dup length HEX: 3f bitand zero-pad-length 0 <string> %
2005-09-10 15:53:17 -04:00
dup length 3 shift 8 >be %
] "" make nip ;
: num-blocks ( length -- num )
2005-09-10 15:53:17 -04:00
-6 shift ;
: get-block ( string num -- string )
2005-09-10 15:53:17 -04:00
6 shift dup 64 + rot <slice> ;
2005-10-16 15:11:50 -04:00
: shift-mod ( n s w -- n )
>r shift r> 1 swap shift 1 - bitand ; inline
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
: hex-string ( str -- str )
2005-09-10 15:53:17 -04:00
[ [ >hex 2 48 pad-left % ] each ] "" make ;