Skip to content

Commit 935e61b

Browse files
committed
FEAT: Allow CHECKSUM again on strings
implements: metaeducation/rebol-issues#1262
1 parent 254b78b commit 935e61b

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/boot/natives.r

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ collect-words: native [
374374

375375
checksum: native [
376376
{Computes a checksum, CRC, or hash.}
377-
data [binary!] {Bytes to checksum}
377+
data [binary! string!] {Bytes to checksum. String value is first converted to UTF-8!}
378378
/part length {Length of data}
379379
/tcp {Returns an Internet TCP 16-bit checksum}
380380
/secure {Returns a cryptographically secure checksum}

src/core/n-strings.c

+13-1
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,22 @@ static struct digest {
194194
REBSER *digest;
195195
REBINT sym = SYM_SHA1;
196196
REBCNT len;
197-
REBYTE *data = VAL_BIN_DATA(arg);
197+
REBYTE *data;
198198

199199
len = Partial1(arg, D_ARG(ARG_CHECKSUM_LENGTH));
200200

201+
if (IS_STRING(arg)) {
202+
// using `digest` just as a temp variable here!
203+
digest = Encode_UTF8_Value(arg, len, 0);
204+
data = SERIES_DATA(digest);
205+
len = SERIES_LEN(digest) - 1;
206+
}
207+
else {
208+
data = VAL_BIN_DATA(arg);
209+
}
210+
211+
212+
201213
// Method word:
202214
if (D_REF(ARG_CHECKSUM_METHOD)) sym = VAL_WORD_CANON(D_ARG(ARG_CHECKSUM_WORD));
203215

src/tests/units/checksum-test.r3

+22
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@ Rebol [
88

99
~~~start-file~~~ "Checksum"
1010

11+
===start-group=== "Checksum of string"
12+
--test-- {checksum of string}
13+
--assert #{C000CBCECDCD2582B0418B8CF0301A8E} = checksum/method "ščř" 'md5
14+
--assert #{900150983CD24FB0D6963F7D28E17F72} = checksum/method "abc" 'md5
15+
--test-- {checksum/part of string}
16+
--assert #{900150983CD24FB0D6963F7D28E17F72} = checksum/method/part "abc123" 'md5 3
17+
--assert #{900150983CD24FB0D6963F7D28E17F72} = checksum/method/part skip "123abc" 3 'md5 3
18+
===end-group===
19+
20+
1121
===start-group=== "Checksum with binary key (issue #1910)"
1222
;@@ https://github.com/rebol/rebol-issues/issues/1910
1323
--test-- "checksum-1"
24+
--assert #{800A1BC1B53CAA795F4DF39DC57652209239E1F1}
25+
= checksum/key "Hello world" "mykey"
1426
--assert #{800A1BC1B53CAA795F4DF39DC57652209239E1F1}
1527
= checksum/key to binary! "Hello world" "mykey"
1628
--assert #{800A1BC1B53CAA795F4DF39DC57652209239E1F1}
@@ -20,6 +32,16 @@ Rebol [
2032

2133
===start-group=== "Checksum basic"
2234
--test-- {checksum ""}
35+
str: ""
36+
--assert #{da39a3ee5e6b4b0d3255bfef95601890afd80709}
37+
= checksum/method str 'sha1
38+
--assert #{e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855}
39+
= checksum/method str 'sha256
40+
--assert #{38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b}
41+
= checksum/method str 'sha384
42+
--assert #{cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e}
43+
= checksum/method str 'sha512
44+
--test-- {checksum #{}}
2345
bin: #{}
2446
--assert #{da39a3ee5e6b4b0d3255bfef95601890afd80709}
2547
= checksum/method bin 'sha1

0 commit comments

Comments
 (0)