mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-18 21:29:19 +02:00
chibi.crypto: code style fixes
- Fixed some typos in sha-native.scm - Removed unnecessary structs and unions from sha_context - Used more efficient implementation of hex32 - Made (scheme base) a common import in (chibi crypto sha2)
This commit is contained in:
parent
9088b1954c
commit
db2b598cde
4 changed files with 22 additions and 28 deletions
|
@ -2,7 +2,7 @@
|
|||
;; Copyright (c) 2015 Alexei Lozovsky. All rights reserved.
|
||||
;; BSD-style license: http://synthcode.com/license.txt
|
||||
|
||||
(define (procees-sha-data! context src)
|
||||
(define (process-sha-data! context src)
|
||||
(cond ((or (bytevector? src) (string? src))
|
||||
(add-sha-data! context src))
|
||||
((input-port? src)
|
||||
|
@ -15,10 +15,10 @@
|
|||
|
||||
(define (sha-224 src)
|
||||
(let ((context (start-sha type-sha-224)))
|
||||
(procees-sha-data! context src)
|
||||
(process-sha-data! context src)
|
||||
(get-sha context)))
|
||||
|
||||
(define (sha-256 src)
|
||||
(let ((context (start-sha type-sha-256)))
|
||||
(procees-sha-data! context src)
|
||||
(process-sha-data! context src)
|
||||
(get-sha context)))
|
||||
|
|
|
@ -54,17 +54,10 @@ struct sha_context {
|
|||
enum sha_type type;
|
||||
char sealed;
|
||||
sexp_uint_t len;
|
||||
union _sha_data {
|
||||
struct _sha_256 {
|
||||
sexp_uint8_t buffer[64];
|
||||
sexp_uint32_t hash[8];
|
||||
} sha_256;
|
||||
} data;
|
||||
sexp_uint32_t hash256[8];
|
||||
sexp_uint8_t buffer[128]; /* enough for all SHA-2 */
|
||||
};
|
||||
|
||||
#define sha_256_buffer(c) ((c)->data.sha_256.buffer)
|
||||
#define sha_256_hash(c) ((c)->data.sha_256.hash)
|
||||
|
||||
/* = SHA-224/256 implementation ===================================== */
|
||||
|
||||
#define ror32(v, a) (((v) >> (a)) | ((v) << (32 - (a))))
|
||||
|
@ -146,10 +139,10 @@ sexp sexp_start_sha (sexp ctx, sexp self, unsigned type, struct sha_context* v)
|
|||
sha->type = type;
|
||||
switch (type) {
|
||||
case SHA_TYPE_224:
|
||||
memcpy(sha_256_hash(sha), h224, sizeof(h224));
|
||||
memcpy(sha->hash256, h224, sizeof(h224));
|
||||
break;
|
||||
case SHA_TYPE_256:
|
||||
memcpy(sha_256_hash(sha), h256, sizeof(h256));
|
||||
memcpy(sha->hash256, h256, sizeof(h256));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
@ -170,20 +163,20 @@ static sexp sha_224_256_add_bytes (struct sha_context *sha,
|
|||
sha->len += len;
|
||||
if (buf_offset) {
|
||||
while ((buf_offset < 64) && (src_offset < len))
|
||||
sha_256_buffer(sha)[buf_offset++] = src[src_offset++];
|
||||
sha->buffer[buf_offset++] = src[src_offset++];
|
||||
if (buf_offset == 64)
|
||||
sha_224_256_round(sha_256_buffer(sha), sha_256_hash(sha));
|
||||
sha_224_256_round(sha->buffer, sha->hash256);
|
||||
else
|
||||
return SEXP_VOID;
|
||||
}
|
||||
/* Process whole chunks without copying them */
|
||||
if (len >= 64) {
|
||||
for ( ; src_offset <= (len - 64); src_offset += 64)
|
||||
sha_224_256_round(src + src_offset, sha_256_hash(sha));
|
||||
sha_224_256_round(src + src_offset, sha->hash256);
|
||||
}
|
||||
/* Copy the remainder into the buffer */
|
||||
if (src_offset < len)
|
||||
memcpy(sha_256_buffer(sha) + buf_offset, src + src_offset, len - src_offset);
|
||||
memcpy(sha->buffer + buf_offset, src + src_offset, len - src_offset);
|
||||
return SEXP_VOID;
|
||||
}
|
||||
|
||||
|
@ -239,8 +232,8 @@ sexp sexp_get_sha (sexp ctx, sexp self, struct sha_context *sha) {
|
|||
switch (sha->type) {
|
||||
case SHA_TYPE_224:
|
||||
case SHA_TYPE_256:
|
||||
sha_224_256_remainder(sha_256_buffer(sha), sha->len % 64,
|
||||
sha->len * 8, sha_256_hash(sha));
|
||||
sha_224_256_remainder(sha->buffer, sha->len % 64,
|
||||
sha->len * 8, sha->hash256);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
@ -248,9 +241,9 @@ sexp sexp_get_sha (sexp ctx, sexp self, struct sha_context *sha) {
|
|||
}
|
||||
switch (sha->type) {
|
||||
case SHA_TYPE_224:
|
||||
return sha_224_256_hash_string(ctx, self, sha_256_hash(sha), 7);
|
||||
return sha_224_256_hash_string(ctx, self, sha->hash256, 7);
|
||||
case SHA_TYPE_256:
|
||||
return sha_224_256_hash_string(ctx, self, sha_256_hash(sha), 8);
|
||||
return sha_224_256_hash_string(ctx, self, sha->hash256, 8);
|
||||
default:
|
||||
return sexp_xtype_exception(ctx, self, "unexpected context type",
|
||||
sexp_make_fixnum(sha->type));
|
||||
|
|
|
@ -41,10 +41,11 @@
|
|||
(arithmetic-shift n (- k))))
|
||||
|
||||
(define (hex32 num)
|
||||
(let ((res (make-string 8 #\0))
|
||||
(num (integer->hex-string num)))
|
||||
(string-copy! res (- 8 (string-length num)) num)
|
||||
res))
|
||||
(let* ((res (number->string num 16))
|
||||
(len (string-length res)))
|
||||
(if (>= len 8)
|
||||
res
|
||||
(string-append (make-string (- 8 len) #\0) res))))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
|
||||
(define-library (chibi crypto sha2)
|
||||
(import (scheme base))
|
||||
(export sha-224 sha-256)
|
||||
(cond-expand
|
||||
(chibi
|
||||
(import (scheme base))
|
||||
(include "sha2-native.scm")
|
||||
(include-shared "crypto"))
|
||||
(else
|
||||
(import (scheme base) (srfi 33) (chibi bytevector))
|
||||
(import (srfi 33) (chibi bytevector))
|
||||
(include "sha2.scm"))))
|
||||
|
||||
;;> \procedure{(sha-224 src)}
|
||||
|
|
Loading…
Add table
Reference in a new issue