1 ! Copyright (C) 2008, 2010, 2016 Slava Pestov, Alexander Ilin
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data checksums checksums.common
4 destructors kernel namespaces openssl openssl.libcrypto sequences ;
7 ERROR: unknown-digest name ;
9 TUPLE: openssl-checksum name ;
11 INSTANCE: openssl-checksum block-checksum
13 CONSTANT: openssl-md5 T{ openssl-checksum f "md5" }
15 CONSTANT: openssl-sha1 T{ openssl-checksum f "sha1" }
17 C: <openssl-checksum> openssl-checksum
21 TUPLE: evp-md-context < disposable handle ;
23 : evp-md-ctx-new ( -- ctx )
24 ssl-new-api? get-global [ EVP_MD_CTX_new ] [ EVP_MD_CTX_create ] if ;
26 : evp-md-ctx-free ( ctx -- )
27 ssl-new-api? get-global [ EVP_MD_CTX_free ] [ EVP_MD_CTX_destroy ] if ;
29 : <evp-md-context> ( -- ctx )
30 evp-md-context new-disposable evp-md-ctx-new >>handle ;
32 M: evp-md-context dispose*
33 handle>> evp-md-ctx-free ;
35 : digest-named ( name -- md )
36 dup EVP_get_digestbyname [ ] [ unknown-digest ] ?if ;
38 : set-digest ( name ctx -- )
39 handle>> swap digest-named f EVP_DigestInit_ex ssl-error ;
41 M: openssl-checksum initialize-checksum-state ( checksum -- evp-md-context )
42 maybe-init-ssl name>> <evp-md-context> [ set-digest ] keep ;
44 M: evp-md-context add-checksum-bytes ( ctx bytes -- ctx' )
45 [ dup handle>> ] dip dup length EVP_DigestUpdate ssl-error ;
47 M: evp-md-context get-checksum ( ctx -- value )
49 { { int EVP_MAX_MD_SIZE } int }
50 [ EVP_DigestFinal_ex ssl-error ] with-out-parameters