Skip to content

Commit

Permalink
retornando possibilidade de hashes para kdf e corrigindo os defeitos …
Browse files Browse the repository at this point in the history
…apontos pelos testes unitários relacionados a este trecho
  • Loading branch information
antonioconselheiro committed Feb 14, 2024
1 parent f0c826f commit 64c5d65
Show file tree
Hide file tree
Showing 32 changed files with 97 additions and 15 deletions.
Empty file modified .vscode/settings.json
100644 → 100755
Empty file.
Empty file modified TODO
100644 → 100755
Empty file.

Large diffs are not rendered by default.

Empty file modified docs/ciphers/test-report.html
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion docs/core/jest-html-reporters-attach/test-report/result.js

Large diffs are not rendered by default.

Empty file modified docs/core/test-report.html
100644 → 100755
Empty file.
Empty file modified package-lock.json
100644 → 100755
Empty file.
8 changes: 6 additions & 2 deletions packages/ciphers/aes/cbc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ class EncryptedURIAESCBCDecrypter extends EncryptedURIDecrypter<TInitializationV
const ivhex = getInitializationVector(this.decoded);
const cipher = base64.decode(this.decoded.cipher);
const params = getSalt(cipher, this.decoded?.params);
console.info(' >>> this.decoded: ', this.decoded);
const derivatedKey = kdf(this.password, params.salt, this.decoded);

const result = await cbc(kdf(this.password, params.salt, this.decoded), hexToBytes(ivhex))
const result = await cbc(derivatedKey, hexToBytes(ivhex))
.decrypt(params.cipher);

return bytesToUtf8(result);
Expand All @@ -46,7 +48,9 @@ class EncryptedURIAESCBCEncrypter extends EncryptedURIEncrypter<TInitializationV
const content = utf8ToBytes(this.params.content);
const saltLength = 8;
const salt = randomBytes(saltLength);
const cipher = await cbc(kdf(this.params.password, salt, this.params), iv).encrypt(content);
console.info(' >>> this.params: ', this.params);
const derivatedKey = kdf(this.params.password, salt, this.params);
const cipher = await cbc(derivatedKey, iv).encrypt(content);

return Promise.resolve({
cipher: base64.encode(OpenSSLSerializer.encode(cipher, salt)),
Expand Down
5 changes: 3 additions & 2 deletions packages/ciphers/aes/kdf.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { HashSupport } from '../hashes/hash-support';
export function kdf<T extends TURIParams>(
password: string,
salt: Uint8Array,
decoded?: TEncryptedURI<T> | TEncryptedURIResultset<T>
kdfConfig?: TEncryptedURI<T> | TEncryptedURIResultset<T>
): Uint8Array {
const cfg = EncryptedURI.getKDFConfig(decoded);
const cfg = EncryptedURI.getKDFConfig(kdfConfig);
console.info(' >>> cfg: ', cfg);

const saltLength = 8;
if (salt.length !== saltLength) {
Expand Down
Empty file modified packages/ciphers/aes/openssl-serializer.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/aes/salt.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/hashes/hash-support.ts
100644 → 100755
Empty file.
13 changes: 12 additions & 1 deletion packages/ciphers/hashes/index.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
export * from './sha256';
export * from './keccak_224';
export * from './keccak_256';
export * from './keccak_384';
export * from './keccak_512';
export * from './sha256';
export * from './sha384';
export * from './sha3_224';
export * from './sha3_256';
export * from './sha3_384';
export * from './sha3_512';
export * from './sha512';
export * from './sha512_256';
4 changes: 4 additions & 0 deletions packages/ciphers/hashes/keccak_224.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { keccak_224 } from '@noble/hashes/sha3';
import { HashSupport } from './hash-support';

HashSupport.addSupport('keccak_224', keccak_224);
4 changes: 4 additions & 0 deletions packages/ciphers/hashes/keccak_256.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { keccak_256 } from '@noble/hashes/sha3';
import { HashSupport } from './hash-support';

HashSupport.addSupport('keccak_256', keccak_256);
5 changes: 5 additions & 0 deletions packages/ciphers/hashes/keccak_384.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { keccak_384 } from '@noble/hashes/sha3';
import { HashSupport } from './hash-support';

HashSupport.addSupport('keccak_384', keccak_384);

5 changes: 5 additions & 0 deletions packages/ciphers/hashes/keccak_512.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { keccak_512 } from '@noble/hashes/sha3';
import { HashSupport } from './hash-support';

HashSupport.addSupport('keccak_512', keccak_512);

Empty file modified packages/ciphers/hashes/sha256.ts
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions packages/ciphers/hashes/sha384.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { sha384 } from '@noble/hashes/sha512'
import { HashSupport } from './hash-support';

HashSupport.addSupport('sha384', sha384);
4 changes: 4 additions & 0 deletions packages/ciphers/hashes/sha3_224.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { sha3_224 } from '@noble/hashes/sha3';
import { HashSupport } from './hash-support';

HashSupport.addSupport('sha3_224', sha3_224);
4 changes: 4 additions & 0 deletions packages/ciphers/hashes/sha3_256.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { sha3_256 } from '@noble/hashes/sha3';
import { HashSupport } from './hash-support';

HashSupport.addSupport('sha3_256', sha3_256);
4 changes: 4 additions & 0 deletions packages/ciphers/hashes/sha3_384.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { sha3_384 } from '@noble/hashes/sha3';
import { HashSupport } from './hash-support';

HashSupport.addSupport('sha3_384', sha3_384);
4 changes: 4 additions & 0 deletions packages/ciphers/hashes/sha3_512.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { sha3_512 } from '@noble/hashes/sha3';
import { HashSupport } from './hash-support';

HashSupport.addSupport('sha3_512', sha3_512);
4 changes: 4 additions & 0 deletions packages/ciphers/hashes/sha512.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { sha512 } from '@noble/hashes/sha512';
import { HashSupport } from './hash-support';

HashSupport.addSupport('sha512', sha512);
4 changes: 4 additions & 0 deletions packages/ciphers/hashes/sha512_256.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { sha512_256 } from '@noble/hashes/sha512';
import { HashSupport } from './hash-support';

HashSupport.addSupport('sha512_256', sha512_256);
Empty file modified packages/ciphers/jest.config.js
100644 → 100755
Empty file.
Empty file modified packages/ciphers/kdf.test.ts
100644 → 100755
Empty file.
Empty file modified packages/ciphers/package-lock.json
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion packages/ciphers/params.test.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EncryptedURI, EncryptedURIParser, TEncryptedURIKDFConfig } from '@encry
import './aes';
import './hashes';

xdescribe('hashing customization', () => {
describe('hashing customization', () => {
it('[3] kdf with hasher sha512', async () => {
const kdf: TEncryptedURIKDFConfig = {
hasher: 'sha512' as any as 'sha256'
Expand Down
25 changes: 24 additions & 1 deletion packages/core/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ describe('EncryptedURI object', () => {
});

describe('EncryptedURI getKDFConfig', () => {
it('[1] EncryptedURI get KDF config from decoded URI', () => {
it('[1] EncryptedURI get KDF config from KDF config', () => {
const configs = EncryptedURI.getKDFConfig({
algorithm: 'aes/cbc',
content: 'mensagem secreta, favor não ler em voz alta',
Expand All @@ -369,4 +369,27 @@ describe('EncryptedURI getKDFConfig', () => {
});

});


it('[2] EncryptedURI get KDF config from decoded URI', () => {
const configs = EncryptedURI.getKDFConfig({
algorithm: 'aes/cbc',
cipher: 'U2FsdGVkX18WeA03azX1tWETWsG/oSiQYzgI0en6RPgQ7Z2i9YbxCL3VcfzL6nsFo5Sdf0xF/UVatnJEehkcHQ==',
queryString: 'iv=44b9c510f05a8461c0ad153ba915d9dc&h=keccak_224',
params: {
iv: '44b9c510f05a8461c0ad153ba915d9dc',
h: 'keccak_224'
}
});

expect(configs).toEqual({
kdf: 'pbkdf2',
ignoreDefaults: true,
hasher: 'keccak_224',
rounds: 32,
derivateKeyLength: 32
});

});

});
9 changes: 3 additions & 6 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type TEncryptedURIKDFConfig = {
*
* @default sha256
*/
hasher?: 'sha256';
hasher?: string | 'sha256' | 'sha512'| 'sha512_256'| 'sha384'| 'sha3_512'| 'sha3_384'| 'sha3_256'| 'sha3_224'| 'keccak_512'| 'keccak_384'| 'keccak_256'| 'keccak_224';

/**
* Iterations of hashing for pbkdf2
Expand Down Expand Up @@ -134,10 +134,7 @@ class EncryptedURIDecoder<T extends TURIParams> {
config.kdf = params.kdf as 'pbkdf2';
}

if (typeof params.h === 'string'
// remove this when this issue is implemented:
// https://github.com/antonioconselheiro/encrypted-uri/issues/27
&& params.h === 'sha256') {
if (typeof params.h === 'string') {
config.hasher = params.h;
}

Expand Down Expand Up @@ -395,7 +392,7 @@ export type TEncryptedURIParams<T extends TURIParams> = {
* number of counts, rounds serialized as string
* this is a pbkdf2 kdf param
*
* @default '1'
* @default '32'
*/
c?: string;

Expand Down
Empty file modified packages/core/package-lock.json
100644 → 100755
Empty file.

0 comments on commit 64c5d65

Please sign in to comment.