mirror of
https://github.com/hanzoai/openid-client.git
synced 2026-09-24 17:01:10 +00:00
24 lines
524 B
JavaScript
24 lines
524 B
JavaScript
'use strict';
|
|
|
|
const base64url = require('base64url');
|
|
const crypto = require('crypto');
|
|
|
|
module.exports = function tokenHash(token, alg) {
|
|
const size = alg.slice(-3);
|
|
let hashingAlg;
|
|
|
|
switch (size) {
|
|
case '512':
|
|
hashingAlg = 'sha512';
|
|
break;
|
|
case '384':
|
|
hashingAlg = 'sha384';
|
|
break;
|
|
default:
|
|
hashingAlg = 'sha256';
|
|
}
|
|
|
|
const digest = crypto.createHash(hashingAlg).update(token).digest('hex');
|
|
return base64url(new Buffer(digest.slice(0, digest.length / 2), 'hex'));
|
|
};
|