sodium-javascript/crypto_hash.js

37 lines
840 B
JavaScript
Raw Normal View History

2020-05-23 11:37:54 +00:00
const sha256 = require('sha256-wasm')
2020-06-16 12:15:33 +00:00
const sha512 = require('sha512-wasm')
2020-05-23 11:37:54 +00:00
var crypto_hash_sha256_BYTES = 32
2020-06-16 12:15:33 +00:00
var crypto_hash_sha512_BYTES = 64
var crypto_hash_BYTES = crypto_hash_sha512_BYTES
2020-05-23 11:37:54 +00:00
2020-06-16 12:15:33 +00:00
function crypto_hash_sha256 (out, m, n) {
check(out, crypto_hash_sha256_BYTES)
2020-05-04 18:52:17 +00:00
2020-06-16 12:15:33 +00:00
sha256().update(m.subarray(0, n)).digest(out)
return 0
2020-05-04 18:52:17 +00:00
}
2020-06-16 12:15:33 +00:00
function crypto_hash_sha512 (out, m, n) {
check(out, crypto_hash_sha512_BYTES)
2020-05-04 18:52:17 +00:00
2020-06-16 12:15:33 +00:00
sha512().update(m.subarray(0, n)).digest(out)
return 0
2020-05-04 18:52:17 +00:00
}
function crypto_hash (out, m, n) {
2020-06-16 12:15:33 +00:00
return crypto_hash_sha512(out, m, n)
2020-05-04 18:52:17 +00:00
}
2020-05-23 11:37:54 +00:00
module.exports = {
crypto_hash,
crypto_hash_sha256,
2020-06-16 12:15:33 +00:00
crypto_hash_sha512,
2020-05-23 11:37:54 +00:00
crypto_hash_BYTES,
crypto_hash_sha256_BYTES
}
2020-06-16 12:15:33 +00:00
function check (buf, len) {
if (!buf || (len && buf.length < len)) throw new Error('Argument must be a buffer' + (len ? ' of length ' + len : ''))
}