source: project/release/5/sha2/trunk/sha2.scm @ 35883

Last change on this file since 35883 was 35883, checked in by Kon Lovett, 15 months ago

add self

File size: 3.5 KB
Line 
1;;;; sha2.scm  -*- Scheme -*-
2;;;; Kon Lovett, Jul '18
3
4#>
5#define SHA2_USE_INTTYPES_H 1
6#define SHA2_USE_MEMSET_MEMCPY 1
7#define SHA2_LITTLE_ENDIAN 1234
8#define SHA2_BIG_ENDIAN    4321
9#ifdef C_BIG_ENDIAN
10# define SHA2_BYTE_ORDER SHA2_BIG_ENDIAN
11#endif
12#ifdef C_LITTLE_ENDIAN
13# define SHA2_BYTE_ORDER SHA2_LITTLE_ENDIAN
14#endif
15
16#include "sha2-base.c"
17
18#undef SHA2_LITTLE_ENDIAN
19#undef SHA2_BIG_ENDIAN
20#undef SHA2_BYTE_ORDER
21#undef SHA2_USE_MEMSET_MEMCPY
22#undef SHA2_USE_INTTYPES_H
23<#
24
25(module sha2
26
27(;exports
28  sha256-primitive sha384-primitive sha512-primitive)
29
30(import scheme
31  (chicken base)
32  (chicken foreign)
33  message-digest-primitive)
34
35;;; 256
36
37(define-foreign-variable context-size-256 int "sizeof(SHA256_CTX)")
38(define-foreign-variable digest-length-256 int "SHA256_DIGEST_LENGTH")
39(define-foreign-variable block-length-256 int "SHA256_BLOCK_LENGTH")
40
41(define sha256-init (foreign-lambda void "SHA256_Init" c-pointer))
42(define sha256-update (foreign-lambda void "SHA256_Update" c-pointer scheme-pointer int))
43(define sha256-final (foreign-lambda void "SHA256_Final" c-pointer scheme-pointer))
44(define sha256-raw-update (foreign-lambda void "SHA256_Update" c-pointer c-pointer int))
45
46(define sha256-primitive
47  (let ((the-sha256-primitive #f))
48    (lambda ()
49      (unless the-sha256-primitive
50        (set! the-sha256-primitive
51              (make-message-digest-primitive
52                context-size-256 digest-length-256 sha256-init sha256-update sha256-final
53                #:block-length block-length-256 #:raw-update sha256-raw-update #:name 'sha256)) )
54      the-sha256-primitive ) ) )
55
56;;; 384
57
58(define-foreign-variable context-size-384 int "sizeof(SHA384_CTX)")
59(define-foreign-variable digest-length-384 int "SHA384_DIGEST_LENGTH")
60(define-foreign-variable block-length-384 int "SHA384_BLOCK_LENGTH")
61
62(define sha384-init (foreign-lambda void "SHA384_Init" c-pointer))
63(define sha384-update (foreign-lambda void "SHA384_Update" c-pointer scheme-pointer int))
64(define sha384-final (foreign-lambda void "SHA384_Final" c-pointer scheme-pointer))
65(define sha384-raw-update (foreign-lambda void "SHA384_Update" c-pointer c-pointer int))
66
67(define sha384-primitive
68  (let ((the-sha384-primitive #f))
69    (lambda ()
70      (unless the-sha384-primitive
71        (set! the-sha384-primitive
72              (make-message-digest-primitive
73                context-size-384 digest-length-384 sha384-init sha384-update sha384-final
74                #:block-length block-length-384 #:raw-update sha384-raw-update #:name 'sha384)) )
75      the-sha384-primitive ) ) )
76
77;;; 512
78
79(define-foreign-variable context-size-512 int "sizeof(SHA512_CTX)")
80(define-foreign-variable digest-length-512 int "SHA512_DIGEST_LENGTH")
81(define-foreign-variable block-length-512 int "SHA512_BLOCK_LENGTH")
82
83(define sha512-init (foreign-lambda void "SHA512_Init" c-pointer))
84(define sha512-update (foreign-lambda void "SHA512_Update" c-pointer scheme-pointer int))
85(define sha512-final (foreign-lambda void "SHA512_Final" c-pointer scheme-pointer))
86(define sha512-raw-update (foreign-lambda void "SHA512_Update" c-pointer c-pointer int))
87
88(define sha512-primitive
89  (let ((the-sha512-primitive #f))
90    (lambda ()
91      (unless the-sha512-primitive
92        (set! the-sha512-primitive
93              (make-message-digest-primitive
94                context-size-512 digest-length-512 sha512-init sha512-update sha512-final
95                #:block-length block-length-512 #:raw-update sha512-raw-update #:name 'sha512)) )
96      the-sha512-primitive ) ) )
97
98) ;module sha2
Note: See TracBrowser for help on using the repository browser.