39 lines
832 B
TypeScript
39 lines
832 B
TypeScript
import crypto from 'crypto'
|
|
|
|
export function hmac(input, key, out) {
|
|
return out
|
|
? crypto.createHmac('sha1', key).update(input).digest(out)
|
|
: crypto.createHmac('sha1', key).update(input).digest('hex')
|
|
}
|
|
|
|
export function genRandomString(length) {
|
|
return crypto
|
|
.randomBytes(Math.ceil(length / 2))
|
|
.toString('hex')
|
|
.slice(0, length)
|
|
}
|
|
|
|
export function sha512(password, salt) {
|
|
let hash = crypto.createHmac('sha512', salt)
|
|
hash.update(password)
|
|
let value = hash.digest('hex')
|
|
return {
|
|
salt: salt,
|
|
passwordHash: value,
|
|
}
|
|
}
|
|
|
|
export function sha1(str) {
|
|
const md5sum = crypto.createHash('sha1')
|
|
md5sum.update(str)
|
|
str = md5sum.digest('hex')
|
|
return str
|
|
}
|
|
|
|
export function md5(str) {
|
|
const md5sum = crypto.createHash('md5')
|
|
md5sum.update(str)
|
|
str = md5sum.digest('hex')
|
|
return str
|
|
}
|