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.
44 lines
1.4 KiB
44 lines
1.4 KiB
2 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')
|
||
|
}
|
||
|
|
||
|
|
||
|
//Old crypto.js file produces different hashes, need to fix before I'll be able to log into my accounts again
|
||
|
export function oldCreateHash(payload) {
|
||
|
let sha256 = crypto.createHash('sha256')
|
||
|
sha256.update(payload)
|
||
|
return sha256.digest('hex')
|
||
|
}
|
||
|
|
||
|
export function oldHmacHex(data, signingKey) {
|
||
|
let hmac = crypto.createHmac('sha256', signingKey)
|
||
|
hmac.update(data)
|
||
|
return hmac.digest('hex')
|
||
|
}
|