Generating the same SHA1 UUID in golang and Javascript

huangapple go评论90阅读模式
英文:

Generating the same SHA1 UUID in golang and Javascript

问题

我有一个我认为很简单的问题。我正在使用以下代码在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")
// 将哈希值转换为UUID字符串

然而,我遇到了严重的问题。看起来Golang中的uuid.Parse函数正在运行这个解析函数,将命名空间转换为一个16字节的数组,所以即使我在JavaScript中使用相同的SHA1算法,我得到的输出也不同。

我一直在尝试在JS中做同样的事情,但是我被卡住了。

这里有没有聪明的加密专家可以帮助我?

英文:

I have what I thought was a pretty simply question. I'm using this code to generate a SHA1 uuid in Golang:

namespace := uuid.Parse("b9cfdb9d-f741-4e1f-89ae-fac6b2a5d740")
sha := uuid.NewSHA1(namespace, []byte("something"))
fmt.Println(sha.String())

Now I want to generate the same UUID in javascript, and I thought it would be as easy as something like this:

var hash = CryptoJS.SHA1("b9cfdb9d-f741-4e1f-89ae-fac6b2a5d740" + "something")
// chomp the hash into a UUID string

However, I'm running into serious issues. It seems that the uuid.Parse function in Golang is running this parsing function that converts the namespace to a 16-byte array, so even though I use the same SHA1 algorithm in Javascript, I'm not getting the same output.

I'v been messing around with doing the same in JS, but I'm stumped.

Any smart crypto people in here that can help me?

答案1

得分: 5

好的,以下是翻译好的代码部分:

var SHA1Generator = {

    hex_chr: "0123456789abcdef",

    hex: function (num) {
        var str = "";
        for (var j = 7; j >= 0; j--)
            str += this.hex_chr.charAt((num >> (j * 4)) & 0x0F);
        return str;
    },


    str2blks_SHA1: function (str) {
        var nblk = ((str.length + 8) >> 6) + 1;
        var blks = new Array(nblk * 16);
        for (var i = 0; i < nblk * 16; i++) blks[i] = 0;
        for (i = 0; i < str.length; i++)
            blks[i >> 2] |= str.charCodeAt(i) << (24 - (i % 4) * 8);
        blks[i >> 2] |= 0x80 << (24 - (i % 4) * 8);
        blks[nblk * 16 - 1] = str.length * 8;
        return blks;
    },


    add: function (x, y) {
        var lsw = (x & 0xFFFF) + (y & 0xFFFF);
        var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
        return (msw << 16) | (lsw & 0xFFFF);
    },


    rol: function (num, cnt) {
        return (num << cnt) | (num >>> (32 - cnt));
    },


    ft: function (t, b, c, d) {
        if (t < 20) return (b & c) | ((~b) & d);
        if (t < 40) return b ^ c ^ d;
        if (t < 60) return (b & c) | (b & d) | (c & d);
        return b ^ c ^ d;
    },


    kt: function (t) {
        return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
         (t < 60) ? -1894007588 : -899497514;
    },

    calcSHA1FromByte: function(byteArr) {
        var str = '';
        for(var i=0; i<byteArr.length; i++)
            str += String.fromCharCode(byteArr[i]);
        return this.calcSHA1(str);
    },

    calcSHA1: function (str) {
        if (str != '') {
            var x = this.str2blks_SHA1(str);
            var w = new Array(80);

            var a = 1732584193;
            var b = -271733879;
            var c = -1732584194;
            var d = 271733878;
            var e = -1009589776;

            for (var i = 0; i < x.length; i += 16) {
                var olda = a;
                var oldb = b;
                var oldc = c;
                var oldd = d;
                var olde = e;

                for (var j = 0; j < 80; j++) {
                    if (j < 16) w[j] = x[i + j];
                    else w[j] = this.rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
                    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)));
                    e = d;
                    d = c;
                    c = this.rol(b, 30);
                    b = a;
                    a = t;
                }

                a = this.add(a, olda);
                b = this.add(b, oldb);
                c = this.add(c, oldc);
                d = this.add(d, oldd);
                e = this.add(e, olde);
            }
            return this.hex(a) + this.hex(b) + this.hex(c) + this.hex(d) + this.hex(e);
        }
        else {
            return '';
        }
    }
};

function stringToByteArray(str) {
  var bytes = [];
  for (var i = 0; i < str.length; ++i) {
      bytes.push(str.charCodeAt(i));
  }
  return bytes;
}

function uuidToByteArray(hex) {

  // If this is a uuid, remove the dashes
  hex = hex.replace(/-/g, "");

  // convert each hex number into a string representation
  // of the byte integer.
  var bytes = [];
  for(var i = 0; i < hex.length; i += 2) {
    bytes.push(parseInt(hex.substring(i, i+2),16));
  }

  return bytes;
}

function sha1ToUUID5(hash) {
  var uuid =  hash.substring(0, 8) +
    '-' + hash.substring(8, 12) +
    // four most significant bits holds version number 5
    '-' + ((parseInt(hash.substring(12, 16), 16) & 0x0fff) | 0x5000).toString(16) +
    // two most significant bits holds zero and one for variant DCE1.1
    '-' + ((parseInt(hash.substring(16, 20), 16) & 0x3fff) | 0x8000).toString(16) +
    '-' + hash.substring(20, 32); //12 digits
  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)))

希望对你有帮助!

英文:

Well, that only took me a month.

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

huangapple
  • 本文由 发表于 2015年10月13日 01:35:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/33087033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定