1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: checksums checksums.md5 combinators grouping kernel math
4 math.bits math.functions sequences splitting ;
9 : lookup-table ( n -- nth )
10 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" nth ; inline
12 : to64 ( v n -- string )
13 [ [ -6 shift ] [ 6 2^ 1 - bitand lookup-table ] bi ]
14 replicate nip ; inline
18 :: passwd-md5 ( magic salt password -- bytes )
19 password magic salt 3append
20 salt password 1surround md5 checksum-bytes
22 [ 16 / ceiling swap <repetition> concat ] keep
24 password [ length make-bits ] [ first ] bi
25 '[ CHAR: \0 _ ? ] "" map-as append
26 md5 checksum-bytes :> final!
31 [ 0 bit? password final ? append ]
32 [ 3 mod 0 > [ salt append ] when ]
33 [ 7 mod 0 > [ password append ] when ]
34 [ 0 bit? final password ? append ]
35 } cleave md5 checksum-bytes final!
38 magic salt "$" 3append
39 { 12 0 6 13 1 7 14 2 8 15 3 9 5 4 10 } final nths 3 group
40 [ first3 [ 16 shift ] [ 8 shift ] bi* + + 4 to64 ] map concat
41 11 final nth 2 to64 3append ;
43 : parse-shadow-password ( string -- magic salt password )
44 "$" split harvest first3 [ "$" 1surround ] 2dip ;
46 : authenticate-password ( shadow password -- ? )
47 '[ parse-shadow-password drop _ passwd-md5 ] keep = ;