猿问

在 golang 和 Javascript 中生成相同的 SHA1 UUID

我有一个我认为很简单的问题。我正在使用此代码在 Golang 中生成 SHA1 uuid:


namespace := uuid.Parse("b9cfdb9d-f741-4e1f-89ae-fac6b2a5d740")

sha := uuid.NewSHA1(namespace, []byte("something"))

fmt.Println(sha.String())

现在我想在 javascript 中生成相同的 UUID,我认为它会像这样简单:


var hash = CryptoJS.SHA1("b9cfdb9d-f741-4e1f-89ae-fac6b2a5d740" + "something")

// chomp the hash into a UUID string

但是,我遇到了严重的问题。uuid.ParseGolang中的函数似乎正在运行这个解析函数,将命名空间转换为 16 字节数组,所以即使我在 Javascript 中使用相同的 SHA1 算法,我也没有得到相同的输出。


我一直在用 JS 做同样的事情,但我很难过。


这里有任何聪明的加密货币人可以帮助我吗?


开满天机
浏览 187回答 1
1回答

繁花不似锦

嗯,那只花了我一个月的时间。var SHA1Generator = {&nbsp; &nbsp; hex_chr: "0123456789abcdef",&nbsp; &nbsp; hex: function (num) {&nbsp; &nbsp; &nbsp; &nbsp; var str = "";&nbsp; &nbsp; &nbsp; &nbsp; for (var j = 7; j >= 0; j--)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str += this.hex_chr.charAt((num >> (j * 4)) & 0x0F);&nbsp; &nbsp; &nbsp; &nbsp; return str;&nbsp; &nbsp; },&nbsp; &nbsp; str2blks_SHA1: function (str) {&nbsp; &nbsp; &nbsp; &nbsp; var nblk = ((str.length + 8) >> 6) + 1;&nbsp; &nbsp; &nbsp; &nbsp; var blks = new Array(nblk * 16);&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < nblk * 16; i++) blks[i] = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (i = 0; i < str.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blks[i >> 2] |= str.charCodeAt(i) << (24 - (i % 4) * 8);&nbsp; &nbsp; &nbsp; &nbsp; blks[i >> 2] |= 0x80 << (24 - (i % 4) * 8);&nbsp; &nbsp; &nbsp; &nbsp; blks[nblk * 16 - 1] = str.length * 8;&nbsp; &nbsp; &nbsp; &nbsp; return blks;&nbsp; &nbsp; },&nbsp; &nbsp; add: function (x, y) {&nbsp; &nbsp; &nbsp; &nbsp; var lsw = (x & 0xFFFF) + (y & 0xFFFF);&nbsp; &nbsp; &nbsp; &nbsp; var msw = (x >> 16) + (y >> 16) + (lsw >> 16);&nbsp; &nbsp; &nbsp; &nbsp; return (msw << 16) | (lsw & 0xFFFF);&nbsp; &nbsp; },&nbsp; &nbsp; rol: function (num, cnt) {&nbsp; &nbsp; &nbsp; &nbsp; return (num << cnt) | (num >>> (32 - cnt));&nbsp; &nbsp; },&nbsp; &nbsp; ft: function (t, b, c, d) {&nbsp; &nbsp; &nbsp; &nbsp; if (t < 20) return (b & c) | ((~b) & d);&nbsp; &nbsp; &nbsp; &nbsp; if (t < 40) return b ^ c ^ d;&nbsp; &nbsp; &nbsp; &nbsp; if (t < 60) return (b & c) | (b & d) | (c & d);&nbsp; &nbsp; &nbsp; &nbsp; return b ^ c ^ d;&nbsp; &nbsp; },&nbsp; &nbsp; kt: function (t) {&nbsp; &nbsp; &nbsp; &nbsp; return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(t < 60) ? -1894007588 : -899497514;&nbsp; &nbsp; },&nbsp; &nbsp; calcSHA1FromByte: function(byteArr) {&nbsp; &nbsp; &nbsp; &nbsp; var str = '';&nbsp; &nbsp; &nbsp; &nbsp; for(var i=0; i<byteArr.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str += String.fromCharCode(byteArr[i]);&nbsp; &nbsp; &nbsp; &nbsp; return this.calcSHA1(str);&nbsp; &nbsp; },&nbsp; &nbsp; calcSHA1: function (str) {&nbsp; &nbsp; &nbsp; &nbsp; if (str != '') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var x = this.str2blks_SHA1(str);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var w = new Array(80);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var a = 1732584193;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var b = -271733879;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var c = -1732584194;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var d = 271733878;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var e = -1009589776;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < x.length; i += 16) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var olda = a;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var oldb = b;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var oldc = c;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var oldd = d;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var olde = e;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var j = 0; j < 80; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (j < 16) w[j] = x[i + j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else w[j] = this.rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t = this.add(this.add(this.rol(a, 5), this.ft(j, b, c, d)), this.add(this.add(e, w[j]), this.kt(j)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e = d;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = c;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c = this.rol(b, 30);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b = a;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a = t;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a = this.add(a, olda);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b = this.add(b, oldb);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c = this.add(c, oldc);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = this.add(d, oldd);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e = this.add(e, olde);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.hex(a) + this.hex(b) + this.hex(c) + this.hex(d) + this.hex(e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return '';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }};function stringToByteArray(str) {&nbsp; var bytes = [];&nbsp; for (var i = 0; i < str.length; ++i) {&nbsp; &nbsp; &nbsp; bytes.push(str.charCodeAt(i));&nbsp; }&nbsp; return bytes;}function uuidToByteArray(hex) {&nbsp; // If this is a uuid, remove the dashes&nbsp; hex = hex.replace(/-/g, "");&nbsp; // convert each hex number into a string representation&nbsp; // of the byte integer.&nbsp; var bytes = [];&nbsp; for(var i = 0; i < hex.length; i += 2) {&nbsp; &nbsp; bytes.push(parseInt(hex.substring(i, i+2),16));&nbsp; }&nbsp; return bytes;}function sha1ToUUID5(hash) {&nbsp; var uuid =&nbsp; hash.substring(0, 8) +&nbsp; &nbsp; '-' + hash.substring(8, 12) +&nbsp; &nbsp; // four most significant bits holds version number 5&nbsp; &nbsp; '-' + ((parseInt(hash.substring(12, 16), 16) & 0x0fff) | 0x5000).toString(16) +&nbsp; &nbsp; // two most significant bits holds zero and one for variant DCE1.1&nbsp; &nbsp; '-' + ((parseInt(hash.substring(16, 20), 16) & 0x3fff) | 0x8000).toString(16) +&nbsp; &nbsp; '-' + hash.substring(20, 32); //12 digits&nbsp; return uuid;}var namespace = "e75a36a9-3323-40dd-a7d1-1c57ad2aa3cd"var id = "event154"var namespaceBytes = uuidToByteArray(namespace);var idBytes = stringToByteArray(id);var allBytes = namespaceBytes.concat(idBytes);console.log("ORG 4505612c-c323-5d6f-b5cc-b7f362b9ba55")console.log("NEW " + sha1ToUUID5(SHA1Generator.calcSHA1FromByte(allBytes)))
随时随地看视频慕课网APP

相关分类

Go
我要回答