我有一些string,我想用使用C#的SHA-256哈希函数来哈希它.我想要这样的东西:
string hashString = sha256_hash("samplestring");
Run Code Online (Sandbox Code Playgroud)
框架中是否有内置功能可以执行此操作?
我从我的python实现获得的HMAC SHA1签名和我的clojure实现略有不同.我很难过会导致这种情况.
Python实现:
import hashlib
import hmac
print hmac.new("my-key", "my-data", hashlib.sha1).hexdigest() # 8bcd5631480093f0b00bd072ead42c032eb31059
Run Code Online (Sandbox Code Playgroud)
Clojure实施:
(ns my-project.hmac
(:import (javax.crypto Mac)
(javax.crypto.spec SecretKeySpec)))
(def algorithm "HmacSHA1")
(defn return-signing-key [key mac]
"Get an hmac key from the raw key bytes given some 'mac' algorithm.
Known 'mac' options: HmacSHA1"
(SecretKeySpec. (.getBytes key) (.getAlgorithm mac)))
(defn sign-to-bytes [key string]
"Returns the byte signature of a string with a given key, using a SHA1 HMAC."
(let [mac (Mac/getInstance algorithm)
secretKey (return-signing-key key mac)]
(-> (doto mac …Run Code Online (Sandbox Code Playgroud)