mirror of
https://framagit.org/framasoft/framadate/funky-framadate-front.git
synced 2023-08-25 13:53:14 +02:00
use crypto lib
This commit is contained in:
parent
5f4c2e04af
commit
1ab5255bfa
@ -1,17 +1,21 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
|
||||
const forge = require('node-forge');
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const crypto = require('crypto');
|
||||
// documentation:
|
||||
// https://medium.com/@spatocode/symmetric-encryption-in-javascript-bcb5fd14c273
|
||||
// https://nodejs.org/api/crypto.html
|
||||
@Component({
|
||||
selector: 'app-ciphering',
|
||||
templateUrl: './ciphering.component.html',
|
||||
styleUrls: ['./ciphering.component.scss'],
|
||||
})
|
||||
export class CipheringComponent implements OnInit {
|
||||
public plainText = 'le texte à chiffrer';
|
||||
public plainText = 'le texte à chiffrer, coucou !';
|
||||
public cipheredText = '';
|
||||
public salt = forge.random.getBytesSync(128);
|
||||
public key = forge.random.getBytesSync(128);
|
||||
algorithm = 'aes-192-cbc';
|
||||
public salt = crypto.randomBytes(16);
|
||||
public initial_vector = crypto.randomBytes(16);
|
||||
public key = '';
|
||||
public otherCipheredText: any;
|
||||
|
||||
constructor() {}
|
||||
@ -21,27 +25,42 @@ export class CipheringComponent implements OnInit {
|
||||
}
|
||||
|
||||
encrypt(someText) {
|
||||
// generate a random key and IV
|
||||
// Note: a key size of 16 bytes will use AES-128, 24 => AES-192, 32 => AES-256
|
||||
// const key = forge.random.getBytesSync(16);
|
||||
// const iv = forge.random.getBytesSync(16);
|
||||
this.key = crypto.scryptSync(someText, 'salt', 24);
|
||||
this.initial_vector = crypto.randomBytes(16);
|
||||
|
||||
/* alternatively, generate a password-based 16-byte key
|
||||
var salt = forge.random.getBytesSync(128);
|
||||
var key = forge.pkcs5.pbkdf2('password', salt, numIterations, 16);
|
||||
*/
|
||||
const cipher = crypto.createCipheriv(this.algorithm, this.key, this.initial_vector);
|
||||
|
||||
cipher.on('readable', () => {
|
||||
this.cipheredText = cipher.read().toString('hex');
|
||||
console.log(cipher.read().toString('hex'));
|
||||
});
|
||||
|
||||
let output = '';
|
||||
|
||||
// encrypt some bytes using CBC mode
|
||||
// (other modes include: ECB, CFB, OFB, CTR, and GCM)
|
||||
// Note: CBC and ECB modes use PKCS#7 padding as default
|
||||
// const cipher = forge.cipher.createCipher('AES-CBC', this.key);
|
||||
// cipher.start({ iv: this.salt });
|
||||
// cipher.update(forge.util.createBuffer(someText));
|
||||
// cipher.finish();
|
||||
// const encrypted = cipher.output;
|
||||
// this.cipheredText = encrypted.toHex();
|
||||
this.cipheredText = someText;
|
||||
// outputs encrypted hex
|
||||
console.log(this.cipheredText);
|
||||
}
|
||||
|
||||
decrypt(sometext) {
|
||||
const decipher = crypto.createDecipheriv(this.algorithm, this.key, this.initial_vector);
|
||||
|
||||
decipher.on('readable', () => {
|
||||
this.otherCipheredText = decipher.read().toString('utf8');
|
||||
|
||||
console.log('otherCipheredText decrypt', this.otherCipheredText);
|
||||
});
|
||||
|
||||
decipher.write(sometext, 'hex');
|
||||
decipher.end();
|
||||
}
|
||||
|
||||
convertTextToInt(someText) {
|
||||
let output = '';
|
||||
|
||||
for (let i = 0; i < someText.length; i++) {
|
||||
output += someText[i].charCodeAt(0);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user