From 4df2e00cbe228a19b10821be5025c0b11aeb27da Mon Sep 17 00:00:00 2001 From: tykayn <15d65f2f-0b14-4f70-bf34-e130180ed62b@users.tedomum.net> Date: Tue, 9 Feb 2021 18:35:29 +0100 Subject: [PATCH] add lib crypto --- package-lock.json | 5 ++ package.json | 1 + .../ciphering/ciphering.component.ts | 58 +++++++++---------- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/package-lock.json b/package-lock.json index 295defef..f676a5b1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6515,6 +6515,11 @@ "which": "^1.2.9" } }, + "crypto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz", + "integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==" + }, "crypto-browserify": { "version": "3.12.0", "resolved": "https://registry.npmjs.org/crypto-browserify/-/crypto-browserify-3.12.0.tgz", diff --git a/package.json b/package.json index 21f26d21..2c419cbf 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "bulma": "^0.9.0", "bulma-switch": "^2.0.0", "chart.js": "^2.9.3", + "crypto": "^1.0.1", "fork-awesome": "^1.1.7", "ng2-charts": "^2.3.0", "ngx-clipboard": "^13.0.0", diff --git a/src/app/features/shared/components/ui/static-pages/ciphering/ciphering.component.ts b/src/app/features/shared/components/ui/static-pages/ciphering/ciphering.component.ts index faa3a095..70ee4d15 100644 --- a/src/app/features/shared/components/ui/static-pages/ciphering/ciphering.component.ts +++ b/src/app/features/shared/components/ui/static-pages/ciphering/ciphering.component.ts @@ -1,6 +1,7 @@ import { Component, OnInit } from '@angular/core'; // eslint-disable-next-line @typescript-eslint/no-var-requires -const crypto = require('crypto'); + +import * as Crypto from 'crypto'; // documentation: // https://medium.com/@spatocode/symmetric-encryption-in-javascript-bcb5fd14c273 // https://nodejs.org/api/crypto.html @@ -13,9 +14,9 @@ export class CipheringComponent implements OnInit { public plainText = 'le texte à chiffrer, coucou !'; public cipheredText = ''; algorithm = 'aes-192-cbc'; - public salt = crypto.randomBytes(16); - public initial_vector = crypto.randomBytes(16); - public key = ''; + // public salt = Crypto.randomBytes(16); + // public initial_vector = Crypto.randomBytes(16); + public key; public otherCipheredText: any; constructor() {} @@ -25,34 +26,33 @@ export class CipheringComponent implements OnInit { } 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')); - }); - - let output = ''; - - this.cipheredText = someText; - // outputs encrypted hex - console.log(this.cipheredText); + // 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); } 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(); + // 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) {