Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Video-Downloader/api/src/misc/crypto.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
27 lines (20 sloc)
929 Bytes
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 { createHmac, createCipheriv, createDecipheriv, randomBytes } from "crypto"; | |
const algorithm = "aes256"; | |
export function generateSalt() { | |
return randomBytes(64).toString('hex'); | |
} | |
export function generateHmac(str, salt) { | |
return createHmac("sha256", salt).update(str).digest("base64url"); | |
} | |
export function encryptStream(plaintext, iv, secret) { | |
const buff = Buffer.from(JSON.stringify(plaintext)); | |
const key = Buffer.from(secret, "base64url"); | |
const cipher = createCipheriv(algorithm, key, Buffer.from(iv, "base64url")); | |
return Buffer.concat([ cipher.update(buff), cipher.final() ]) | |
} | |
export function decryptStream(ciphertext, iv, secret) { | |
const buff = Buffer.from(ciphertext); | |
const key = Buffer.from(secret, "base64url"); | |
const decipher = createDecipheriv(algorithm, key, Buffer.from(iv, "base64url")); | |
return Buffer.concat([ decipher.update(buff), decipher.final() ]) | |
} |