Skip to content

Instantly share code, notes, and snippets.

@id
Created August 29, 2023 06:58
Show Gist options
  • Save id/e94cba0583debf38692d4b83dd360eb4 to your computer and use it in GitHub Desktop.
Save id/e94cba0583debf38692d4b83dd360eb4 to your computer and use it in GitHub Desktop.
Encrypt in Typescript --> Decrypt with openssl
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