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.
43 lines
870 B
43 lines
870 B
import { createHmac, randomBytes } from 'node:crypto'
|
|
|
|
/**
|
|
* TokenHelper
|
|
*
|
|
* @author Björn Hase <me@herr-hase.wtf>
|
|
* @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3
|
|
* @link https://git.node001.net/HerrHase/signpost.git
|
|
*
|
|
*/
|
|
const TokenHelper = {
|
|
|
|
/**
|
|
* create hash from token
|
|
*
|
|
* @param string token
|
|
*
|
|
* @return string
|
|
*
|
|
*/
|
|
create(token) {
|
|
const hmac = createHmac(process.env.APP_HASH_TYPE, process.env.APP_SALT)
|
|
const buffer = new Buffer(token)
|
|
|
|
return hmac.update(buffer).digest('base64')
|
|
},
|
|
|
|
/**
|
|
* check if token and hash are equal
|
|
*
|
|
* @param string token
|
|
* @param string hash
|
|
*
|
|
* @return boolean
|
|
*
|
|
*/
|
|
equal(token, hash) {
|
|
return TokenHelper.create(token) === hash
|
|
}
|
|
}
|
|
|
|
export default TokenHelper
|