mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-04-22 18:52:18 +00:00
40 lines
755 B
JavaScript
40 lines
755 B
JavaScript
'use strict'
|
|
|
|
const nodeify = require('nodeify')
|
|
const Buffer = require('safe-buffer').Buffer
|
|
|
|
const crypto = require('./webcrypto')()
|
|
const lengths = require('./hmac-lengths')
|
|
|
|
const hashTypes = {
|
|
SHA1: 'SHA-1',
|
|
SHA256: 'SHA-256',
|
|
SHA512: 'SHA-512'
|
|
}
|
|
|
|
exports.create = function (hashType, secret, callback) {
|
|
const hash = hashTypes[hashType]
|
|
|
|
nodeify(crypto.subtle.importKey(
|
|
'raw',
|
|
secret,
|
|
{
|
|
name: 'HMAC',
|
|
hash: {name: hash}
|
|
},
|
|
false,
|
|
['sign']
|
|
).then((key) => {
|
|
return {
|
|
digest (data, cb) {
|
|
nodeify(crypto.subtle.sign(
|
|
{name: 'HMAC'},
|
|
key,
|
|
data
|
|
).then((raw) => Buffer.from(raw)), cb)
|
|
},
|
|
length: lengths[hashType]
|
|
}
|
|
}), callback)
|
|
}
|