UTF-8 Hashing
I got a bug filed for my extension, since I am using the native hash functionalities right from the XPCOM (@mozilla.org/security/hash;1) without any code from me between it, I wonder how to solve this: md5 hash a UTF-8 encoded string: "您好"(it means "hello" in Chinese), it yields a different result from python hashlib and unix md5sum when being processed by the XPCOM object @mozilla.org/security/hash;1 Any thought how to go about this? -- dev-tech-crypto mailing list dev-tech-crypto@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-tech-crypto
Re: UTF-8 Hashing
If it helps, here is the code I currently utilize; hash: function(str,method) { var hash_engine = Components.classes["@mozilla.org/security/hash; 1"].createInstance().QueryInterface (Components.interfaces.nsICryptoHash); switch(method) { case 'MD5': hash_engine.init(hash_engine.MD5); break; case 'SHA1': hash_engine.init(hash_engine.SHA1); break; case 'SHA256': hash_engine.init(hash_engine.SHA256); break; } var charcodes = []; for (var i = 0; i < str.length; i++){ charcodes.push(str.charCodeAt(i)); } hash_engine.update(charcodes, str.length); return TOOLS.convert('bin2hex',hash_engine.finish(false)); }, -- dev-tech-crypto mailing list dev-tech-crypto@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-tech-crypto
Re: UTF-8 Hashing
On Apr 23, 3:39 pm, Jean-Marc Desperrier wrote: > Nelson B Bolyard wrote: > > Is that python code? I thought it was JavaScript. > > Yes, you're right, I had a really too quick look at it :-) On a second thought, I just had a look at this page: https://developer.mozilla.org/En/NsICryptoHash Where it states that one must use the scriptable unicode converter first to get bytes back that were UTF-8 encoded: var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]. createInstance(Components.interfaces.nsIScriptableUnicodeConverter); // we use UTF-8 here, you can choose other encodings. converter.charset = "UTF-8"; I guess that's what I missed? /rvdh -- dev-tech-crypto mailing list dev-tech-crypto@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-tech-crypto
Re: UTF-8 Hashing
Right, this seems to work very well. I consider it solved. Here is the corrected code: hash: function(str,method) { var converter = Components.classes["@mozilla.org/intl/ scriptableunicodeconverter"].createInstance (Components.interfaces.nsIScriptableUnicodeConverter); converter.charset = "UTF-8"; var result = {}; var data = converter.convertToByteArray(str, result); var hash_engine = Components.classes["@mozilla.org/security/hash; 1"].createInstance().QueryInterface (Components.interfaces.nsICryptoHash); switch(method) { case 'MD5': hash_engine.init(hash_engine.MD5); break; case 'SHA1': hash_engine.init(hash_engine.SHA1); break; case 'SHA256': hash_engine.init(hash_engine.SHA256); break; } hash_engine.update(data, result.value); return TOOLS.convert('bin2hex',hash_engine.finish(false)); }, -- dev-tech-crypto mailing list dev-tech-crypto@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-tech-crypto