funky-framadate-front/src/app/features/shared/components/ui/static-pages/ciphering/ciphering.component.ts

191 lines
5.0 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import CipherLib from './lib_cipher';
// eslint-disable-next-line @typescript-eslint/no-var-requires
// documentation:
// https://medium.com/@spatocode/symmetric-encryption-in-javascript-bcb5fd14c273
// https://nodejs.org/api/crypto.html
// https://preview.npmjs.com/package/ecma-nacl
@Component({
selector: 'app-ciphering',
templateUrl: './ciphering.component.html',
styleUrls: ['./ciphering.component.scss'],
})
export class CipheringComponent implements OnInit {
public plainText = 'le texte à chiffrer, coucou !';
// public plainText = '1234';
public cipheredText =
'121-127-42-110-127-42-122-121-115-128-124-111-125-107-118-127-126-42-118-111-125-42-122-115-122-121-118-111';
algorithm = 'aes-192-cbc';
public salt = 'ou du poivre heh';
public initial_vector = '';
public key;
public otherCipheredText = 'le texte à chiffrer, coucou !';
constructor() {}
ngOnInit(): void {
this.encrypt(this.plainText);
}
// ---------------------- make unreadable ----------------------
get simpleCipheredText() {
return this.convertTextToInt(this.plainText);
}
get simpleCipheredTextWithSalt() {
return this.convertTextToInt(this.salt + this.plainText);
}
get simpleCipheredTextWithSpreadSalt() {
return this.convertTextToIntMixedSalt(this.salt + this.plainText);
}
get simpleCipheredTextWithHashedSalt() {
return this.convertTextToIntMixedSalt(this.md5(this.salt) + this.plainText);
}
get simpleCipheredTextWithHashedSpreadSalt() {
return this.convertTextToIntMixedSalt(this.md5(this.salt) + this.plainText);
}
get layerOnSpread() {
return this.convertIntToText(this.simpleCipheredTextWithHashedSalt, false);
}
// ---------------------- make readable ----------------------
get simpleDeCipheredText() {
return this.convertIntToText(this.cipheredText);
}
get simpleDeCipheredTextWithSalt() {
return this.convertIntToText(this.salt + this.cipheredText);
}
get simpleDeCipheredTextWithSpreadSalt() {
return this.convertIntToTextMixedSalt(this.salt + this.cipheredText);
}
// ---------------------- conversions ----------------------
encrypt(someText) {
// this.key = Crypto.scryptSync(someText, 'salt', 24);
// this.initial_vector = Crypto.randomBytes(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'));
// });
//
//
// this.cipheredText = someText;
// // outputs encrypted hex
// console.log(this.cipheredText);
return someText;
}
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();
return sometext;
}
convertTextToInt(value) {
let result = '';
for (let i = 0; i < value.length; i++) {
if (i < value.length - 1) {
result += value.charCodeAt(i) + 10;
result += '-';
} else {
result += value.charCodeAt(i) + 10;
}
}
return result;
}
convertIntToText(value, removeBeginning = true) {
let result = '';
const array = value.split('-');
for (let i = 0; i < array.length; i++) {
result += String.fromCharCode(array[i] - 10);
}
console.info('value, result', value, result);
// remove salt characters
if (removeBeginning) {
result = result.slice(this.salt.length, -1);
}
return result;
}
convertTextToIntMixedSalt(value) {
let result = '';
let saltIndex = 0;
for (let i = 0; i < value.length; i++) {
if (i < value.length - 1) {
result += value.charCodeAt(i) + 10 + this.salt.charCodeAt(saltIndex);
result += '-';
} else {
result += value.charCodeAt(i) + 10 + this.salt.charCodeAt(saltIndex);
}
saltIndex++;
if (saltIndex >= this.salt.length) {
saltIndex = 0;
}
}
return result;
}
convertIntToTextMixedSalt(value) {
let result = '';
const array = value.split('-');
let saltIndex = -1;
for (let i = 0; i < array.length; i++) {
// console.log(
// 'encodage',
// i,
// value.charCodeAt(i),
// saltIndex,
// this.salt.charCodeAt(saltIndex),
// value.charCodeAt(i) + 10 + this.salt.charCodeAt(saltIndex)
// );
result += String.fromCharCode(array[i] - 10 - this.salt.charCodeAt(saltIndex));
if (saltIndex > this.salt.length) {
saltIndex = 0;
} else {
saltIndex++;
}
}
// remove salt characters
return result.slice(this.salt.length);
}
md5(someText) {
return CipherLib.md5(someText);
}
getEntropy(s) {
let sum = 0,
len = s.length;
this.process(s, (k, f) => {
const p = f / len;
sum -= (p * Math.log(p)) / Math.log(2);
});
return sum;
}
process(s, evaluator) {
let h = Object.create(null),
k;
s.split('').forEach((c) => {
(h[c] && h[c]++) || (h[c] = 1);
});
if (evaluator) for (k in h) evaluator(k, h[k]);
return h;
}
}