import { createDecipheriv, createHmac, scryptSync, } from 'crypto' import { AuthValidateResponse } from './types' export const decryptHash = ({ secret, apiKey, auth, key, }: { secret: string, apiKey: string, auth: string, key: Buffer, }) => { try { const iv = scryptSync(secret, apiKey, 16) // generate the same ciphertext every time const algorithm = 'aes-256-cbc' // algorithm to use const decipher = createDecipheriv(algorithm, key, iv) const decrypted = decipher.update(auth, 'hex', 'utf8') + decipher.final('utf8') return { success: true, ...JSON.parse(decrypted), } } catch (error) { return { success: false, } } } export const compareHashes = (authHash: string, key: Buffer) => { const newHash = createHmac('sha256', key).digest('hex') if (authHash === newHash) return true return false } export const errorMessage = (response: AuthValidateResponse) => { response.error = 'Not Authorised, Log In' response.access = false return response }