在 Factor 中获得与 coreutils sha224sum 相同的 SHA-224 和

Get the same SHA-224 sum in Factor as coreutils sha224sum

$ echo *
a b c
$ cat *
file 1
file 2
file 3
$ factor -e=" \ 
> USING: globs io sequences sorting io.files io.encodings.utf8 ; \ 
> \"*\" glob natural-sort [ utf8 file-lines ] map concat [ print ] each "
file 1
file 2
file 3

使用 Factor 的 glob 和 shell 的 glob 的输出相同。输出上的 diff 显示它们完全匹配。

$ factor -e=" \
> USING: math.parser checksums checksums.sha globs io sequences sorting io.files io.encodings.utf8 ; \
> \"*\" glob natural-sort [ utf8 file-lines ] map concat sha-224 checksum-lines bytes>hex-string print "

0feaf7d5c46b802404760778091ed1312ba82d4206b9f93c35570a1a
$ cat * | sha224sum
d1240479399e5a37f8e62e2935a7ac4b9352e41d6274067b27a36101

但是校验和不匹配,md5 校验和也不匹配。为什么是这样?如何在 Factor 中获得与 coreutils 中相同的校验和 sha224sum?

将编码更改为 ascii 不会更改输出,"\n" join sha-224 checksum-bytes 也不会更改 checksum-lines

这种奇怪的行为是由于校验和行中的错误造成的。 factor/factor#1708

感谢 jonenst for finding the problem, and calsioro 在 Factor 邮件列表中提供此代码:

This code:

[
    { "a" "b" "c" } 3 [1,b]
    [ number>string "file " prepend [ write ] curry
      ascii swap with-file-writer ] 2each

    "*" glob natural-sort [ utf8 file-lines ] map concat
    [ "\n" append ] map "" join  ! Add newlines between and at the end

    sha-224 checksum-bytes bytes>hex-string print
] with-test-directory

gives the same hash:

d1240479399e5a37f8e62e2935a7ac4b9352e41d6274067b27a36101

jonenst also pointed out 即:

Also, regarding the three different lengths you get, "exercism/self-update/self-update.factor" is missing a '\n' char at the end of the last line. That's why you get surprising results.

如果您尝试对文件进行校验和,请确保它们都以尾随换行符结尾。