source: project/release/4/sha2/trunk/sha2.scm @ 15618

Last change on this file since 15618 was 15618, checked in by Kon Lovett, 11 years ago

Initial save.

File size: 3.3 KB
Line 
1;;;; sha2.scm
2
3(module sha2 (;export
4  sha256-binary-digest sha384-binary-digest sha512-binary-digest
5  sha256-digest sha384-digest sha512-digest
6  sha256-primitive sha384-primitive sha512-primitive)
7
8  (import chicken scheme foreign)
9  (require-extension message-digest)
10
11  (declare
12    (fixnum)
13    (no-procedure-checks) )
14
15#>
16#define SHA2_USE_INTTYPES_H 1
17#define SHA2_USE_MEMSET_MEMCPY 1
18#define SHA2_LITTLE_ENDIAN 1234
19#define SHA2_BIG_ENDIAN    4321
20#ifdef C_BIG_ENDIAN
21# define SHA2_BYTE_ORDER SHA2_BIG_ENDIAN
22#endif
23#ifdef C_LITTLE_ENDIAN
24# define SHA2_BYTE_ORDER SHA2_LITTLE_ENDIAN
25#endif
26
27#include "sha2-base.c"
28
29#undef SHA2_LITTLE_ENDIAN
30#undef SHA2_BIG_ENDIAN
31#undef SHA2_BYTE_ORDER
32#undef SHA2_USE_MEMSET_MEMCPY
33#undef SHA2_USE_INTTYPES_H
34#endif
35<#
36
37;;; 256
38
39(define-foreign-variable context-size-256 int "sizeof(SHA256_CTX)")
40
41(define sha256-init (foreign-lambda void "SHA256_Init" c-pointer))
42(define sha256-update (foreign-lambda void "SHA256_Update" c-pointer pointer int))
43(define sha256-final (foreign-lambda void "SHA256_Final" c-pointer pointer))
44
45(define-constant digest-length-256 32) ; SHA256_DIGEST_LENGTH
46
47(define (sha256-binary-digest obj)
48  (make-binary-message-digest obj
49    context-size-256 digest-length-256
50    sha256-init sha256-update sha256-final
51    'sha256-binary-digest))
52
53(define (sha256-digest obj)
54  (make-message-digest obj
55    context-size-256 digest-length-256
56    sha256-init sha256-update sha256-final
57    'sha256-digest))
58
59(define (sha256-primitive)
60  (make-message-digest-primitive
61    context-size-384 digest-length-384
62    sha384-init sha384-update sha384-final
63    'sha256-primitive))
64
65;;; 384
66
67(define-foreign-variable context-size-384 int "sizeof(SHA384_CTX)")
68
69(define sha384-init (foreign-lambda void "SHA384_Init" c-pointer))
70(define sha384-update (foreign-lambda void "SHA384_Update" c-pointer pointer int))
71(define sha384-final (foreign-lambda void "SHA384_Final" c-pointer pointer))
72
73(define-constant digest-length-384 48) ; SHA384_DIGEST_LENGTH
74
75(define (sha384-binary-digest obj)
76  (make-binary-message-digest obj
77    context-size-384 digest-length-384
78    sha384-init sha384-update sha384-final
79    'sha384-binary-digest))
80
81(define (sha384-digest obj)
82  (make-message-digest obj
83    context-size-384 digest-length-384
84    sha384-init sha384-update sha384-final
85    'sha384-digest))
86
87(define (sha384-primitive)
88  (make-message-digest-primitive
89    context-size-256 digest-length-256
90    sha256-init sha256-update sha256-final
91    'sha384-primitive))
92
93;;; 512
94
95(define-foreign-variable context-size-512 int "sizeof(SHA512_CTX)")
96
97(define sha512-init (foreign-lambda void "SHA512_Init" c-pointer))
98(define sha512-update (foreign-lambda void "SHA512_Update" c-pointer pointer int))
99(define sha512-final (foreign-lambda void "SHA512_Final" c-pointer pointer))
100
101(define-constant digest-length-512 64) ; SHA512_DIGEST_LENGTH
102
103(define (sha512-binary-digest obj)
104  (make-binary-message-digest obj
105    context-size-512 digest-length-512
106    sha512-init sha512-update sha512-final
107    'sha512-binary-digest))
108
109(define (sha512-digest obj)
110  (make-message-digest obj
111    context-size-512 digest-length-512
112    sha512-init sha512-update sha512-final
113    'sha512-digest))
114
115(define (sha512-primitive)
116  (make-message-digest-primitive
117    context-size-512 digest-length-512
118    sha512-init sha512-update sha512-final
119    'sha512-primitive))
120
121) ;module sha2
Note: See TracBrowser for help on using the repository browser.