我想使用racket在stunk中散列stdin但是找不到Python的hashlib的sha1.update函数是否有等价或替代?
答案 0 :(得分:2)
查看Greg Hendershott的sha
库,通过为SHA-1的OpenSSL实现提供Racket接口,为SHA-1和SHA-2添加Racket支持, SHA-2。
(require sha)
(sha1 bstr) -> sha1?
bstr : bytes?
虽然它没有update()
程序,如Python自己的documentation所述,这样的调用:
m.update(a); m.update(b)
等同于:
m.update(a+b)
答案 1 :(得分:2)
这是一种方法:
#lang racket
(require (planet soegaard/digest:1:2/digest))
(define a #"The quick brown fox jumps ")
(define b #"over the lazy dog")
(define a+b (bytes-append a b))
(digest a+b 'sha1)
(define c (make-digest-context 'sha1))
(update-context c a)
(update-context c b)
(final-context->hex-string c)
输出结果为:
"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"
"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"
您可以在此处找到digest
集合的文档:
http://planet.racket-lang.org/package-source/soegaard/digest.plt/1/2/doc.txt
使用类似这样的东西:
(define c (make-digest-context 'sha1))
(define in (open-input-file "foo"))
(let loop ()
(define chunk (read-bytes 4096 in))
(cond [(eof-object? chunk) (final-context->hex-string c)]
[else (update-context c chunk)
(loop)]))