Created
August 29, 2023 02:03
-
-
Save hyrious/8e4baed8ad213c7a78ebf9f6957b19ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var IDX = 256, HEX: string[] = []; | |
while (IDX--) HEX[IDX] = (IDX + 256).toString(16).substring(1); | |
/** | |
* @param algorithm only "SHA-1", "SHA-256", "SHA-384", "SHA-512". | |
* | |
* **Note**: "SHA-1" is not recommended. | |
*/ | |
export async function digestHex(algorithm: AlgorithmIdentifier, data: string | BufferSource): Promise<string> { | |
if (typeof data === "string") data = new TextEncoder().encode(data); | |
var buf = await crypto.subtle.digest(algorithm, data); | |
var ary = new Uint8Array(buf), str = '', len = ary.length; | |
for (var i = 0; i < len; ++i) str += HEX[ary[i]]; | |
return str; | |
} | |
// digestHex('SHA-1', 'hello world').then(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment