You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
1007 B
30 lines
1007 B
3 years ago
|
import crypto from 'crypto' // Does not work on client because this is a Node library, but works for below server-only functions
|
||
|
// These libraries are old but they work and can be included on both server and client
|
||
|
import shajs from 'sha.js'
|
||
|
import hmac from 'hash.js/lib/hash/hmac.js'
|
||
|
import sha256 from 'hash.js/lib/hash/sha/256.js' // Only works for shorter hashes, not in createHash used for hashing meme files
|
||
|
|
||
|
export function createHash(payload) {
|
||
|
return shajs('sha256').update(payload).digest('hex')
|
||
|
}
|
||
|
|
||
|
export function hmacHex(data, signingKey) {
|
||
|
return hmac(sha256, signingKey).update(data).digest('hex')
|
||
|
}
|
||
|
|
||
|
export function derivePublicKey(p) {
|
||
|
return crypto.createPublicKey(p).export({
|
||
|
type: 'spki',
|
||
|
format: 'pem',
|
||
|
})
|
||
|
}
|
||
|
|
||
|
export function encryptToPublic(pub, info) {
|
||
|
return crypto.publicEncrypt(pub, new Buffer(info)).toString('hex')
|
||
|
}
|
||
|
|
||
|
export function decryptFromPrivate(priv, hiddenInfo) {
|
||
|
return crypto
|
||
|
.privateDecrypt(priv, Buffer.from(hiddenInfo, 'hex'))
|
||
|
.toString('latin1')
|
||
|
}
|