How Can I Encript String To SHA-1 Using Javascript OR Jquery?
I want to hash a simple string with SHA-1 hash function using javascript.
Solution 1:
In a browser, you can use SubtleCrypto.digest, which returns a Promise:
crypto.subtle.digest('SHA-1', arrayBuffer);
In node.js you can use crypto.createHash:
const crypto = require( 'crypto' );
const hash = crypto.createHash('sha1');
const result = hash.digest(buffer);
Browser demo:
logSha1( 'foobar' );
async function logSha1( str ) {
const buffer = new TextEncoder( 'utf-8' ).encode( str );
const digest = await crypto.subtle.digest('SHA-1', buffer);
// Convert digest to hex string
const result = Array.from(new Uint8Array(digest)).map( x => x.toString(16).padStart(2,'0') ).join('');
console.log( result );
}
Post a Comment for "How Can I Encript String To SHA-1 Using Javascript OR Jquery?"