UNPKG

smartid-calls

Version:

Smart-ID client module for Node.JS with proxy layer

19 lines (14 loc) 746 B
import * as crypto from 'crypto'; class AuthHash { public static async generateRandomHash(hashType = 'sha512'): Promise<{ raw: Buffer, digest: string }> { const buf = await crypto.randomBytes(64); return { raw: buf, digest: crypto.createHash(hashType).update(buf).digest('base64') } } public static calculateVerificationCode(digestBase64: string, hashType = 'sha512'): string { const sha256HashedInput = crypto.createHash(hashType).update(Buffer.from(digestBase64, 'base64')).digest(); const integer = sha256HashedInput.readUIntBE(sha256HashedInput.length-2, 2); const verificationCode = String('0000' + (integer % 10000).toString().substr(-4)).slice(-4); return verificationCode; } } export default AuthHash;