User password encrypted

This commit is contained in:
Eudes Inácio
2021-08-26 16:32:59 +01:00
parent 246b626600
commit c80d8fb588
3 changed files with 75 additions and 1 deletions
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { AESEncrypt } from './aesencrypt.service';
describe('AuthService', () => {
let service: AESEncrypt;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(AESEncrypt);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
+56
View File
@@ -0,0 +1,56 @@
import { Injectable } from '@angular/core';
import CryptoJS from 'crypto-js';
@Injectable({
providedIn: 'root'
})
export class AESEncrypt {
ivArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
constructor() { }
encrypt(encryptData, pass) {
var text = "Pro-it te espera!!!!";
//Creating the Vector Key
var iv = CryptoJS.enc.Hex.parse(this.toHexString(this.ivArray));
//Encoding the Password in from UTF8 to byte array
var Pass = CryptoJS.enc.Utf8.parse(pass);
//Encoding the Salt in from UTF8 to byte array
var Salt = CryptoJS.enc.Utf8.parse("gabinetedigital");
//Creating the key in PBKDF2 format to be used during the decryption
var key128Bits1000Iterations = CryptoJS.PBKDF2(Pass.toString(CryptoJS.enc.Utf8), Salt, { keySize: 128 / 32, iterations: 1000 });
//Decrypting the string contained in cipherParams using the PBKDF2 key
var decrypted = CryptoJS.AES.encrypt(encryptData, key128Bits1000Iterations, { mode: CryptoJS.mode.CBC, iv: iv, padding: CryptoJS.pad.Pkcs7 });
console.log('AES encrypt',decrypted.toString());
}
decrypt(deceyptData,pass) {
//Creating the Vector Key
var iv = CryptoJS.enc.Hex.parse(this.toHexString(this.ivArray));
//Encoding the Password in from UTF8 to byte array
var Pass = CryptoJS.enc.Utf8.parse(pass);
//Encoding the Salt in from UTF8 to byte array
var Salt = CryptoJS.enc.Utf8.parse("gabinetedigital");
//Creating the key in PBKDF2 format to be used during the decryption
var key128Bits1000Iterations = CryptoJS.PBKDF2(Pass.toString(CryptoJS.enc.Utf8), Salt, { keySize: 128 / 32, iterations: 1000 });
//Enclosing the test to be decrypted in a CipherParams object as supported by the CryptoJS libarary
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(deceyptData)
})
//Decrypting the string contained in cipherParams using the PBKDF2 key
var decrypted = CryptoJS.AES.decrypt(cipherParams, key128Bits1000Iterations, { mode: CryptoJS.mode.CBC, iv: iv, padding: CryptoJS.pad.Pkcs7 });
console.log('AES decrypt',decrypted.toString(CryptoJS.enc.Utf8));
}
toHexString(byteArray) {
return Array.from(byteArray, (value: any) => {
return ('0' + (value & 0xFF).toString(16)).slice(-2);
}).join('')
}
}
+3 -1
View File
@@ -12,6 +12,7 @@ import { LocalstoreService } from '../store/localstore.service';
import { ToastService } from './toast.service';
import { UserStore } from 'src/app/store/user.service'
import { SHA1, SHA256, AES, enc } from 'crypto-js'
import { AESEncrypt } from '../services/aesencrypt.service';
@Injectable({
providedIn: 'root'
@@ -33,6 +34,7 @@ export class AuthService {
public alertController: AlertController,
private localstoreService: LocalstoreService,
private toastService: ToastService,
private aesencrypt: AESEncrypt,
) {
this.headers = new HttpHeaders();
@@ -51,7 +53,7 @@ export class AuthService {
async login(user: UserForm): Promise<boolean> {
// user.BasicAuthKey = 'Basic ' + btoa(user.username + '@' + user.domainName + ':' + user.password);
user.BasicAuthKey = 'Basic ' + btoa(user.username + ':' + user.password); //conversão em base64 das credenciais inseridas
console.log('Basic ' + btoa(user.username + ':' + SHA1(user.password).toString())); //conversão em base64 das credenciais inseridas
console.log('Basic ' + btoa(user.username + ':' + this.aesencrypt.encrypt(user.password,user.username ))); //conversão em base64 das credenciais inseridas
this.headers = this.headers.set('Authorization',user.BasicAuthKey);