//////////////HASH, cifrar,descrifrar y base64 export function sha512(str) { let result = crypto.subtle.digest("SHA-512", new TextEncoder("utf-8").encode(str)).then(buf => { return Array.prototype.map.call(new Uint8Array(buf), x=>(('00'+x.toString(16)).slice(-2))).join(''); }); return result; } export function cifrar(ficheroTextoPlano, clave){ //Devuelve un objeto cryptojs, al hacer toSterng devolvera el formato json con todo let salt = CryptoJS.lib.WordArray.random(128 / 8); //Generamos salt let iv = CryptoJS.lib.WordArray.random(128 / 8); //Generamos vector de ini let keyderivation = CryptoJS.PBKDF2(clave, salt, { //Derivamos de la clave keySize: 256 / 32, iterations: 100 }); let encrypted = CryptoJS.AES.encrypt(ficheroTextoPlano, keyderivation, { //Fichero a cifrar, clave derivada, IV, relleno PKcs7 y el modo CBC iv: iv, padding: CryptoJS.pad.Pkcs7, mode: CryptoJS.mode.CBC, format: JsonFormatter }); let objectCJS = JSON.parse(encrypted.toString()); objectCJS.s = salt.toString(); return JSON.stringify(objectCJS); } export function descifrar(ficheroCifrado, clave){ //Devuelve la cadena descifrada let ficherocifradoJS = JSON.parse(ficheroCifrado); let salt = CryptoJS.enc.Hex.parse(ficherocifradoJS.s); let iv = CryptoJS.enc.Hex.parse(ficherocifradoJS.iv) let keyderivation = CryptoJS.PBKDF2(clave, salt, { //Derivamos de la clave keySize: 256 / 32, iterations: 100 }); let decrypted = CryptoJS.AES.decrypt(ficherocifradoJS.ct, keyderivation, { iv: iv, padding: CryptoJS.pad.Pkcs7, mode: CryptoJS.mode.CBC }) return decrypted.toString(CryptoJS.enc.Utf8); } export function utf8_to_b64( str ) { return window.btoa(unescape(encodeURIComponent( str ))); } export function b64_to_utf8( str ) { return decodeURIComponent(escape(window.atob( str ))); } export function generarParDeClaves(){ return crypto.subtle.generateKey( { name: "RSA-OAEP", modulusLength: 4096, publicExponent: new Uint8Array([1, 0, 1]), hash: "SHA-512" //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512" }, true, ["encrypt", "decrypt"] ); } export function exportarClave(key){ return crypto.subtle.exportKey( "jwk", // JSON Web Key key ); } export function importarClavePublica(jsonKey){ return crypto.subtle.importKey( "jwk", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only) jsonKey, { //these are the algorithm options name: "RSA-OAEP", hash: {name: "SHA-512"}, }, true, //SI se va a poder exportar ["encrypt"] //"encrypt" or "wrapKey" for public key import or //"decrypt" or "unwrapKey" for private key imports ); } export function importarClavePrivada(jsonKey){ return crypto.subtle.importKey( "jwk", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only) jsonKey, { //these are the algorithm options name: "RSA-OAEP", hash: {name: "SHA-512"}, }, true, //SI se va a poder exportar ["decrypt"] //"encrypt" or "wrapKey" for public key import or //"decrypt" or "unwrapKey" for private key imports ); } export function cifrarConPublica(publicKey, data){ return crypto.subtle.encrypt( { name: "RSA-OAEP", }, publicKey, //from generateKey or importKey above data //ArrayBuffer of data you want to encrypt ); } export function descifrarConPrivada(privateKey, data){ return crypto.subtle.decrypt( { name: "RSA-OAEP", }, privateKey, //from generateKey or importKey above data //ArrayBuffer of the data ); } export function CodificarDatos(data) { let enc = new TextEncoder(); return enc.encode(data); } export function DecodificarDatos(data) { let enc = new TextDecoder(); return enc.decode(data); } var JsonFormatter = { stringify: function(cipherParams) { // create json object with ciphertext var jsonObj = { ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64) }; // optionally add iv or salt if (cipherParams.iv) { jsonObj.iv = cipherParams.iv.toString(); } if (cipherParams.salt) { jsonObj.s = cipherParams.salt.toString(); } // stringify json object return JSON.stringify(jsonObj); } }; export function sha256(m){ return CryptoJS.SHA256("hola").toString(); }