(stringToEncrypt: string)
| 29 | |
| 30 | // Encrypt token with password using crypto.Cipheriv |
| 31 | export const encryptToken = (stringToEncrypt: string) => { |
| 32 | const key = crypto.createHash('sha256').update(getTokenHashSecret()).digest() |
| 33 | |
| 34 | const IV_LENGTH = 16 |
| 35 | const iv = crypto.randomBytes(IV_LENGTH) |
| 36 | const cipher = crypto.createCipheriv('aes-256-cbc', key, iv) |
| 37 | const encrypted = cipher.update(stringToEncrypt) |
| 38 | |
| 39 | const result = Buffer.concat([encrypted, cipher.final()]) |
| 40 | |
| 41 | // formatted string [iv]:[token] |
| 42 | return iv.toString('hex') + ':' + result.toString('hex') |
| 43 | } |
| 44 | |
| 45 | // Decrypt token using the inverse of encryption crypto algorithm |
| 46 | export const decryptToken = (stringToDecrypt: string): string | undefined => { |
no test coverage detected