Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created February 12, 2022 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/89f64be230b975b9552269f0dd7cf42a to your computer and use it in GitHub Desktop.
Save parzibyte/89f64be230b975b9552269f0dd7cf42a to your computer and use it in GitHub Desktop.
const encriptar = async (contraseña, textoPlano) => {
const encoder = new TextEncoder();
const sal = window.crypto.getRandomValues(new Uint8Array(16));
const vectorInicializacion = window.crypto.getRandomValues(new Uint8Array(16));
const bufferTextoPlano = encoder.encode(textoPlano);
const clave = await derivacionDeClaveBasadaEnContraseña(contraseña, sal, 100000, 256, 'SHA-256');
const encrypted = await window.crypto.subtle.encrypt(
{ name: "AES-CBC", iv: vectorInicializacion },
clave,
bufferTextoPlano
);
return bufferABase64([
...sal,
...vectorInicializacion,
...new Uint8Array(encrypted)
]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment