sodium-javascript/crypto_hash.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-08-27 22:52:54 +00:00
const sha512 = require('sha512-universal')
2020-06-17 15:27:28 +00:00
const assert = require('nanoassert')
2020-05-23 11:37:54 +00:00
if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.')
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_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_sha512_state () {
return sha512()
}
function crypto_hash_sha512_update (state, m, n) {
if (n === undefined) n = m.byteLength
state.update(m.subarray(0, n))
return 0
}
function crypto_hash_sha512_final (state, out) {
assert(out.byteLength === crypto_hash_sha512_BYTES, "out must be 'crypto_hash_sha512_BYTES' bytes long")
state.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,
2020-06-16 12:15:33 +00:00
crypto_hash_sha512,
crypto_hash_sha512_state,
crypto_hash_sha512_update,
crypto_hash_sha512_final,
crypto_hash_sha512_BYTES,
crypto_hash_BYTES
2020-05-23 11:37:54 +00:00
}