2016-09-13 13:23:11 +02:00
|
|
|
'use strict'
|
|
|
|
|
2017-08-17 06:38:26 +02:00
|
|
|
const nodeify = require('../nodeify')
|
2016-09-13 13:23:11 +02:00
|
|
|
|
2019-01-08 18:37:03 +00:00
|
|
|
const crypto = require('../webcrypto')
|
2017-07-22 10:57:27 -07:00
|
|
|
const lengths = require('./lengths')
|
2016-09-13 13:23:11 +02:00
|
|
|
|
|
|
|
const hashTypes = {
|
|
|
|
SHA1: 'SHA-1',
|
|
|
|
SHA256: 'SHA-256',
|
|
|
|
SHA512: 'SHA-512'
|
|
|
|
}
|
|
|
|
|
2017-08-17 06:38:26 +02:00
|
|
|
const sign = (key, data, cb) => {
|
2019-01-03 08:13:07 -08:00
|
|
|
nodeify(crypto.subtle.sign({ name: 'HMAC' }, key, data)
|
2017-08-17 06:38:26 +02:00
|
|
|
.then((raw) => Buffer.from(raw)), cb)
|
|
|
|
}
|
|
|
|
|
2016-09-13 13:23:11 +02:00
|
|
|
exports.create = function (hashType, secret, callback) {
|
|
|
|
const hash = hashTypes[hashType]
|
|
|
|
|
|
|
|
nodeify(crypto.subtle.importKey(
|
|
|
|
'raw',
|
|
|
|
secret,
|
|
|
|
{
|
|
|
|
name: 'HMAC',
|
2019-01-03 08:13:07 -08:00
|
|
|
hash: { name: hash }
|
2016-09-13 13:23:11 +02:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
['sign']
|
|
|
|
).then((key) => {
|
|
|
|
return {
|
|
|
|
digest (data, cb) {
|
2017-08-17 06:38:26 +02:00
|
|
|
sign(key, data, cb)
|
2016-09-13 13:23:11 +02:00
|
|
|
},
|
|
|
|
length: lengths[hashType]
|
|
|
|
}
|
|
|
|
}), callback)
|
|
|
|
}
|