Created
August 29, 2023 06:58
-
-
Save id/e94cba0583debf38692d4b83dd360eb4 to your computer and use it in GitHub Desktop.
Encrypt in Typescript --> Decrypt with openssl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as crypto from 'crypto'; | |
function encryptToken(token: string, password: string): string { | |
const iv = crypto.randomBytes(16); | |
let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(password), iv); | |
let encrypted = cipher.update(token); | |
encrypted = Buffer.concat([encrypted, cipher.final()]); | |
return `${iv.toString('hex')}:${encrypted.toString('hex')}`; | |
} | |
--- | |
iv=$(echo "$encrypted_token" | cut -d':' -f1) | |
encrypted=$(echo "$encrypted_token" | cut -d':' -f2) | |
token=$(echo "$encrypted" | xxd -ps -r | openssl enc -aes-256-cbc -d -K $(echo "$password" | xxd -ps -c 999) -iv "$iv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment