mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-04-25 12:12:18 +00:00
* fix: add buffer, cleanup, reduce size - add buffer related to https://github.com/ipfs/js-ipfs/issues/2924 - remove unnecessary eslint ignore - remove tweelnacl and use node-forge - remove browserify-aes and use node-forge - use multibase to encode b58 - require only sha256 from multihashing - reduce bundle size after all the deps here https://github.com/ipfs/js-ipfs/issues/2924 are merged libp2p-crypto will be able to be bundle with `node: false` 🎉 * fix: reduce bundle size * fix: use new secp * fix: bundle size * chore: update secp Co-Authored-By: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com>
37 lines
736 B
JavaScript
37 lines
736 B
JavaScript
'use strict'
|
|
const { Buffer } = require('buffer')
|
|
const webcrypto = require('../webcrypto')
|
|
const lengths = require('./lengths')
|
|
|
|
const hashTypes = {
|
|
SHA1: 'SHA-1',
|
|
SHA256: 'SHA-256',
|
|
SHA512: 'SHA-512'
|
|
}
|
|
|
|
const sign = async (key, data) => {
|
|
return Buffer.from(await webcrypto.get().subtle.sign({ name: 'HMAC' }, key, data))
|
|
}
|
|
|
|
exports.create = async function (hashType, secret) {
|
|
const hash = hashTypes[hashType]
|
|
|
|
const key = await webcrypto.get().subtle.importKey(
|
|
'raw',
|
|
secret,
|
|
{
|
|
name: 'HMAC',
|
|
hash: { name: hash }
|
|
},
|
|
false,
|
|
['sign']
|
|
)
|
|
|
|
return {
|
|
async digest (data) { // eslint-disable-line require-await
|
|
return sign(key, data)
|
|
},
|
|
length: lengths[hashType]
|
|
}
|
|
}
|