1 ! Copyright (C) 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors byte-arrays alien.c-types kernel continuations
4 destructors sequences io openssl openssl.libcrypto checksums
8 ERROR: unknown-digest name ;
10 TUPLE: openssl-checksum name ;
12 : openssl-md5 T{ openssl-checksum f "md5" } ;
14 : openssl-sha1 T{ openssl-checksum f "sha1" } ;
16 INSTANCE: openssl-checksum stream-checksum
18 C: <openssl-checksum> openssl-checksum
22 TUPLE: evp-md-context handle ;
24 : <evp-md-context> ( -- ctx )
25 "EVP_MD_CTX" <c-object>
26 dup EVP_MD_CTX_init evp-md-context boa ;
28 M: evp-md-context dispose
29 handle>> EVP_MD_CTX_cleanup drop ;
31 : with-evp-md-context ( quot -- )
32 maybe-init-ssl [ <evp-md-context> ] dip with-disposal ; inline
34 : digest-named ( name -- md )
35 dup EVP_get_digestbyname
36 [ ] [ unknown-digest ] ?if ;
38 : set-digest ( name ctx -- )
39 handle>> swap digest-named f EVP_DigestInit_ex ssl-error ;
41 : checksum-loop ( ctx -- )
43 4096 read-partial dup [
44 dup length EVP_DigestUpdate ssl-error
48 : digest-value ( ctx -- value )
50 EVP_MAX_MD_SIZE <byte-array> 0 <int>
51 [ EVP_DigestFinal_ex ssl-error ] 2keep
52 *int memory>byte-array ;
56 M: openssl-checksum checksum-stream