From 54b80a4223beefcbe09877f81febcaf59d2fe2d6 Mon Sep 17 00:00:00 2001 From: Alexander Iljin Date: Thu, 23 Jun 2016 10:10:57 +0300 Subject: [PATCH] checksums.openssl: implement common checksum interface --- basis/checksums/openssl/openssl.factor | 42 +++++++++++--------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/basis/checksums/openssl/openssl.factor b/basis/checksums/openssl/openssl.factor index 41c8537d45..7752340502 100644 --- a/basis/checksums/openssl/openssl.factor +++ b/basis/checksums/openssl/openssl.factor @@ -1,8 +1,8 @@ -! Copyright (C) 2008, 2010 Slava Pestov +! Copyright (C) 2008, 2010, 2016 Slava Pestov, Alexander Ilin ! See http://factorcode.org/license.txt for BSD license. -USING: accessors byte-arrays alien.c-types alien.data kernel -continuations destructors sequences io openssl openssl.libcrypto -checksums checksums.stream classes.struct ; +USING: accessors alien.c-types alien.data checksums +checksums.stream destructors io kernel openssl openssl.libcrypto +sequences ; IN: checksums.openssl ERROR: unknown-digest name ; @@ -17,8 +17,6 @@ INSTANCE: openssl-checksum stream-checksum C: openssl-checksum - ( -- ctx ) @@ -28,8 +26,7 @@ TUPLE: evp-md-context < disposable handle ; M: evp-md-context dispose* handle>> EVP_MD_CTX_destroy ; -: with-evp-md-context ( quot -- ) - maybe-init-ssl [ ] dip with-disposal ; inline +> swap digest-named f EVP_DigestInit_ex ssl-error ; -: checksum-loop ( ctx -- ) - dup handle>> - 4096 read-partial dup [ - dup length EVP_DigestUpdate ssl-error - checksum-loop - ] [ 3drop ] if ; +PRIVATE> -: digest-value ( ctx -- value ) +M: openssl-checksum initialize-checksum-state ( checksum -- evp-md-context ) + maybe-init-ssl name>> [ set-digest ] keep ; + +M: evp-md-context add-checksum-bytes ( ctx bytes -- ctx' ) + [ dup handle>> ] dip dup length EVP_DigestUpdate ssl-error ; + +M: evp-md-context get-checksum ( ctx -- value ) handle>> { { int EVP_MAX_MD_SIZE } int } [ EVP_DigestFinal_ex ssl-error ] with-out-parameters memory>byte-array ; -PRIVATE> +M: openssl-checksum checksum-bytes ( bytes checksum -- value ) + initialize-checksum-state [ swap add-checksum-bytes get-checksum ] with-disposal ; -M: openssl-checksum checksum-stream - name>> swap [ - [ - [ set-digest ] - [ checksum-loop ] - [ digest-value ] - tri - ] with-evp-md-context - ] with-input-stream ; +M: openssl-checksum checksum-stream ( stream checksum -- value ) + initialize-checksum-state [ swap add-checksum-stream get-checksum ] with-disposal ;