diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2019-08-29 06:58:27 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2019-08-29 06:58:27 -0700 |
commit | b5c9b7ffd95b2934e503e8524ac0f571ed2b9f91 (patch) | |
tree | ebbaaf5b94e21eb7fed1bd07363d264209ad7f9e | |
parent | 0689ed6d0d0d74f9ca62057290db3bda8622afba (diff) | |
download | txr-b5c9b7ffd95b2934e503e8524ac0f571ed2b9f91.tar.gz txr-b5c9b7ffd95b2934e503e8524ac0f571ed2b9f91.tar.bz2 txr-b5c9b7ffd95b2934e503e8524ac0f571ed2b9f91.zip |
sha256/md5: allow characters and byte values.
* chksum.c (sha256_utf8_byte_callback, md5_utf8_byte_callback):
New static functions.
(sha256_hash, md5_hash): Support character and integer
objects.
* txr.1: Documented.
-rw-r--r-- | chksum.c | 42 | ||||
-rw-r--r-- | txr.1 | 10 |
2 files changed, 48 insertions, 4 deletions
@@ -183,6 +183,14 @@ val sha256_begin(void) return cobj(coerce(mem_t *, ps256), sha256_ctx_s, &sha256_ops); } +static int sha256_utf8_byte_callback(int b, mem_t *ctx) +{ + SHA256_t *ps256 = coerce(SHA256_t *, ctx); + unsigned char uc = b; + SHA256_update(ps256, &uc, 1); + return 1; +} + val sha256_hash(val ctx, val obj) { val self = lit("sha256-hash"); @@ -201,6 +209,19 @@ val sha256_hash(val ctx, val obj) case BUF: sha256_szmax_upd(ps256, obj->b.data, c_unum(obj->b.len)); break; + case CHR: + utf8_encode(c_chr(obj), sha256_utf8_byte_callback, coerce(mem_t *, ps256)); + break; + case NUM: + { + cnum n = c_num(obj); + unsigned char uc = n; + if (n < 0 || n > 255) + uw_throwf(error_s, lit("~a: byte value ~s out of range"), + self, obj, nao); + SHA256_update(ps256, &uc, 1); + } + break; default: uw_throwf(error_s, lit("~a: cannot hash ~s, only buffer and strings"), self, obj, nao); @@ -424,6 +445,14 @@ val md5_begin(void) return cobj(coerce(mem_t *, pmd5), md5_ctx_s, &md5_ops); } +static int md5_utf8_byte_callback(int b, mem_t *ctx) +{ + MD5_t *ps256 = coerce(MD5_t *, ctx); + unsigned char uc = b; + MD5_update(ps256, &uc, 1); + return 1; +} + val md5_hash(val ctx, val obj) { val self = lit("md5-hash"); @@ -442,6 +471,19 @@ val md5_hash(val ctx, val obj) case BUF: md5_szmax_upd(pmd5, obj->b.data, c_unum(obj->b.len)); break; + case CHR: + utf8_encode(c_chr(obj), md5_utf8_byte_callback, coerce(mem_t *, pmd5)); + break; + case NUM: + { + cnum n = c_num(obj); + unsigned char uc = n; + if (n < 0 || n > 255) + uw_throwf(error_s, lit("~a: byte value ~s out of range"), + self, obj, nao); + MD5_update(pmd5, &uc, 1); + } + break; default: uw_throwf(error_s, lit("~a: cannot hash ~s, only buffer and strings"), self, obj, nao); @@ -52411,8 +52411,9 @@ by including .meta obj into the digest calculation. The .meta obj -argument may be a character string, whose UTF-8 representation is digested, -or a buffer object, whose contents are digested. +argument may be: a character or character string, whose UTF-8 representation is +digested; a buffer object, whose contents are digested; or an integer, +representing a byte value in the range 0 to 255 included in the digest. The .code sha256-hash function may be called multiple times to include any mixture of @@ -52467,8 +52468,9 @@ by including .meta obj into the digest calculation. The .meta obj -argument may be a character string, whose UTF-8 representation is digested, -or a buffer object, whose contents are digested. +argument may be: a character or character string, whose UTF-8 representation is +digested; a buffer object, whose contents are digested; or an integer, +representing a byte value in the range 0 to 255 included in the digest. The .code md5-hash function may be called multiple times to include any mixture of |