Searched this for a while and didn't find a good answer. Then finally stumbled upon someone else who pointed out that Browsers have built in crypto functions.
Here is the example code. Only works on HTTPS pages.
let message = "Test string for SHA1";
let msgUint8 = new TextEncoder().encode(message);
let hashBuffer = await crypto.subtle.digest('SHA-1', msgUint8);
let hashArray = Array.from(new Uint8Array(hashBuffer));
let sha1 = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
I tested this against PHP sha() and get the same values.
PHP :
365525a324d3c73e01574500261d5642e1b8ea33
JS:
365525a324d3c73e01574500261d5642e1b8ea33
MDN page on this:
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
No comments:
Post a Comment