sodium-javascript/crypto_hash.js

34 lines
861 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-06-17 15:27:28 +00:00
const assert = require('nanoassert')
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) {
2020-06-17 15:27:28 +00:00
assert(out.byteLength === crypto_hash_sha256_BYTES, "out must be 'crypto_hash_sha256_BYTES' bytes long")
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) {
2020-06-17 15:27:28 +00:00
assert(out.byteLength === crypto_hash_sha512_BYTES, "out must be 'crypto_hash_sha512_BYTES' bytes long")
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
}