Skip to content

Commit

Permalink
incluindo configuração que permite ignorar a inclusão do algoritmo pa…
Browse files Browse the repository at this point in the history
…drão (aes/cbc)
  • Loading branch information
antonioconselheiro committed Feb 15, 2024
1 parent bf9bfa4 commit 9a310dc
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 41 deletions.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/core/jest-html-reporters-attach/test-report/result.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions packages/ciphers/aes/cbc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class EncryptedURIAESCBCDecrypter extends EncryptedURIDecrypter<TInitializationV
const cipher = base64.decode(this.decoded.cipher);
const params = getSalt(cipher, this.decoded?.params);
const derivatedKey = kdf(this.password, params.salt, this.decoded);

const result = await cbc(derivatedKey, hexToBytes(ivhex))
.decrypt(params.cipher);

Expand All @@ -47,7 +46,6 @@ class EncryptedURIAESCBCEncrypter extends EncryptedURIEncrypter<TInitializationV
const content = utf8ToBytes(this.params.content);
const saltLength = 8;
const salt = randomBytes(saltLength);

const derivatedKey = kdf(this.params.password, salt, this.params);
const cipher = await cbc(derivatedKey, iv).encrypt(content);

Expand Down
10 changes: 6 additions & 4 deletions packages/ciphers/aes/ctr/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EncryptedURIAlgorithm, EncryptedURIDecrypter, EncryptedURIEncrypter, TEncryptedURI, TEncryptedURIResultset } from "@encrypted-uri/core";
import { bytesToUtf8, hexToBytes, utf8ToBytes } from "@noble/ciphers/utils";
import { EncryptedURIAlgorithm, EncryptedURIDecrypter, EncryptedURIEncrypter, TEncryptedURI, TEncryptedURIResultset } from '@encrypted-uri/core';
import { bytesToUtf8, hexToBytes, utf8ToBytes } from '@noble/ciphers/utils';
import { ctr } from '@noble/ciphers/webcrypto/aes';
import { randomBytes } from "@noble/hashes/utils";
import { base64 } from '@scure/base';
Expand All @@ -20,7 +20,8 @@ class EncryptedURIAESCTRDecrypter extends EncryptedURIDecrypter<TInitializationV
const ivhex = getInitializationVector(this.decoded);
const cipher = base64.decode(this.decoded.cipher);
const params = getSalt(cipher, this.decoded?.params);
const result = await ctr(kdf(this.password, params.salt, this.decoded), hexToBytes(ivhex))
const derivatedKey = kdf(this.password, params.salt, this.decoded);
const result = await ctr(derivatedKey, hexToBytes(ivhex))
.decrypt(params.cipher);

return bytesToUtf8(result);
Expand All @@ -46,7 +47,8 @@ class EncryptedURIAESCTREncrypter extends EncryptedURIEncrypter<TInitializationV
const content = utf8ToBytes(this.params.content);
const saltLength = 8;
const salt = randomBytes(saltLength);
const cipher = await ctr(kdf(this.params.password, salt, this.params), iv).encrypt(content);
const derivatedKey = kdf(this.params.password, salt, this.params);
const cipher = await ctr(derivatedKey, iv).encrypt(content);

return Promise.resolve({
cipher: base64.encode(OpenSSLSerializer.encode(cipher, salt)),
Expand Down
6 changes: 4 additions & 2 deletions packages/ciphers/aes/ecb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class EncryptedURIAESECBDecrypter<T extends TURIParams = TURIParams> extends Enc
async decrypt(): Promise<string> {
const cipher = base64.decode(this.decoded.cipher || '');
const params = getSalt(cipher, this.decoded?.params);
const result = await ecb(kdf(this.password, params.salt, this.decoded))
const derivatedKey = kdf(this.password, params.salt, this.decoded);
const result = await ecb(derivatedKey)
.decrypt(params.cipher);

return bytesToUtf8(result);
Expand All @@ -42,7 +43,8 @@ class EncryptedURIAESECBEncrypter<T extends TURIParams = TURIParams> extends Enc
const content = utf8ToBytes(this.params.content);
const saltLength = 8;
const salt = randomBytes(saltLength);
const rawCipher = await ecb(kdf(this.params.password, salt, this.params)).encrypt(content);
const derivatedKey = kdf(this.params.password, salt, this.params);
const rawCipher = await ecb(derivatedKey).encrypt(content);
const cipher = base64.encode(OpenSSLSerializer.encode(rawCipher, salt));

return Promise.resolve({ cipher });
Expand Down
6 changes: 4 additions & 2 deletions packages/ciphers/aes/gcm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class EncryptedURIAESGCMDecrypter extends EncryptedURIDecrypter<TNumberOnceParam
const nonce = getNumberOnce(this.decoded);
const cipher = base64.decode(this.decoded.cipher);
const params = getSalt(cipher, this.decoded?.params);
const result = await gcm(kdf(this.password, params.salt, this.decoded), hexToBytes(nonce))
const derivatedKey = kdf(this.password, params.salt, this.decoded);
const result = await gcm(derivatedKey, hexToBytes(nonce))
.decrypt(params.cipher);

return bytesToUtf8(result);
Expand All @@ -46,7 +47,8 @@ class EncryptedURIAESGCMEncrypter extends EncryptedURIEncrypter<TNumberOnceParam
const content = utf8ToBytes(this.params.content);
const saltLength = 8;
const salt = randomBytes(saltLength);
const cipher = await gcm(kdf(this.params.password, salt, this.params), nonce).encrypt(content);
const derivatedKey = kdf(this.params.password, salt, this.params);
const cipher = await gcm(derivatedKey, nonce).encrypt(content);

return Promise.resolve({
cipher: base64.encode(OpenSSLSerializer.encode(cipher, salt)),
Expand Down
16 changes: 9 additions & 7 deletions packages/ciphers/aes/siv/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { EncryptedURIAlgorithm, EncryptedURIDecrypter, EncryptedURIEncrypter, TEncryptedURI, TEncryptedURIResultset } from "@encrypted-uri/core";
import { EncryptedURIAlgorithm, EncryptedURIDecrypter, EncryptedURIEncrypter, TEncryptedURI, TEncryptedURIResultset } from '@encrypted-uri/core';
import { siv } from '@noble/ciphers/aes';
import { bytesToUtf8, hexToBytes, utf8ToBytes } from '@noble/ciphers/utils';
import { randomBytes } from "@noble/hashes/utils";
import { randomBytes } from '@noble/hashes/utils';
import { base64 } from '@scure/base';
import { kdf } from "../kdf";
import { kdf } from '../kdf';
import { TNumberOnceParams, getNumberOnce } from '../number-once';
import { OpenSSLSerializer } from "../openssl-serializer";
import { getSalt } from "../salt";
import { OpenSSLSerializer } from '../openssl-serializer';
import { getSalt } from '../salt';

class EncryptedURIAESSIVDecrypter extends EncryptedURIDecrypter<TNumberOnceParams> {
constructor(
Expand All @@ -20,7 +20,8 @@ class EncryptedURIAESSIVDecrypter extends EncryptedURIDecrypter<TNumberOnceParam
const nonce = getNumberOnce(this.decoded);
const cipher = base64.decode(this.decoded.cipher);
const params = getSalt(cipher, this.decoded?.params);
const result = await siv(kdf(this.password, params.salt, this.decoded), hexToBytes(nonce))
const derivatedKey = kdf(this.password, params.salt, this.decoded);
const result = await siv(derivatedKey, hexToBytes(nonce))
.decrypt(params.cipher);

return bytesToUtf8(result);
Expand All @@ -46,7 +47,8 @@ class EncryptedURIAESSIVEncrypter extends EncryptedURIEncrypter<TNumberOnceParam
const content = utf8ToBytes(this.params.content);
const saltLength = 8;
const salt = randomBytes(saltLength);
const cipher = await siv(kdf(this.params.password, salt, this.params), nonce).encrypt(content);
const derivatedKey = kdf(this.params.password, salt, this.params);
const cipher = await siv(derivatedKey, nonce).encrypt(content);

return Promise.resolve({
cipher: base64.encode(OpenSSLSerializer.encode(cipher, salt)),
Expand Down
6 changes: 3 additions & 3 deletions packages/ciphers/params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('hashing customization', () => {

const parser = new EncryptedURIParser(encoded);
expect(parser.decoded.params?.kdf).toEqual(undefined);
expect(parser.decoded.algorithm).toEqual('aes/cbc');
expect(parser.decoded.algorithm).toEqual(undefined);
expect(parser.decoded.params?.c).toEqual(undefined);
expect(parser.decoded.params?.dklen).toEqual(undefined);
expect(parser.decoded.params?.h).toEqual('keccak_512');
Expand All @@ -204,7 +204,7 @@ describe('hashing customization', () => {

const parser = new EncryptedURIParser(encoded);
expect(parser.decoded.params?.kdf).toEqual(undefined);
expect(parser.decoded.algorithm).toEqual('aes/cbc');
expect(parser.decoded.algorithm).toEqual(undefined);
expect(parser.decoded.params?.c).toEqual(undefined);
expect(parser.decoded.params?.dklen).toEqual(undefined);
expect(parser.decoded.params?.h).toEqual('keccak_384');
Expand Down Expand Up @@ -301,7 +301,7 @@ describe('checking if params are correctly encoded', () => {

const parser = new EncryptedURIParser(encoded);
expect(parser.decoded.params?.kdf).toEqual(undefined);
expect(parser.decoded.algorithm).toEqual('aes/cbc');
expect(parser.decoded.algorithm).toEqual(undefined);
expect(parser.decoded.params?.c).toEqual(undefined);
expect(parser.decoded.params?.dklen).toEqual(undefined);
expect(parser.decoded.params?.h).toEqual(undefined);
Expand Down
20 changes: 9 additions & 11 deletions packages/core/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ describe('decode uri with default values', () => {
});

it('[3] decode uri with some default values not include', () => {
expect(new EncryptedURIParser('encrypted:aes/cbc?2345678wertyui;en1e3kj3e31jn2algoritmgenerateddata').decoded)
expect(new EncryptedURIParser('encrypted:?2345678wertyui;en1e3kj3e31jn2algoritmgenerateddata').decoded)
.toEqual({
algorithm: 'aes/cbc',
cipher: 'en1e3kj3e31jn2algoritmgenerateddata',
queryString: '2345678wertyui'
});
Expand Down Expand Up @@ -52,9 +51,8 @@ describe('decode uri with default values', () => {
});

it('[6] decode uri with some default values not include', () => {
expect(new EncryptedURIParser('encrypted:aes/cbc?iv=2345678wertyui&pad=pkcs%237;en1e3kj3e31jn2algoritmgenerateddata').decoded)
expect(new EncryptedURIParser('encrypted:?iv=2345678wertyui&pad=pkcs%237;en1e3kj3e31jn2algoritmgenerateddata').decoded)
.toEqual({
algorithm: 'aes/cbc',
cipher: 'en1e3kj3e31jn2algoritmgenerateddata',
queryString: 'iv=2345678wertyui&pad=pkcs%237',
params: {
Expand Down Expand Up @@ -126,7 +124,7 @@ describe('encode uri with configs using default values', () => {
params: {
iv: '2345678wertyui'
}
}).encoded).toEqual('encrypted:aes/cbc?iv=2345678wertyui;en1e3kj3e31jn2algoritmgenerateddata')
}).encoded).toEqual('encrypted:?iv=2345678wertyui;en1e3kj3e31jn2algoritmgenerateddata')
});

it('[3] encode with default config with default values', () => {
Expand All @@ -142,7 +140,7 @@ describe('encode uri with configs using default values', () => {
algorithm: 'aes/cbc',
cipher: 'en1e3kj3e31jn2algoritmgenerateddata',
queryString: '2345678wertyui'
}).encoded).toEqual('encrypted:aes/cbc?2345678wertyui;en1e3kj3e31jn2algoritmgenerateddata')
}).encoded).toEqual('encrypted:?2345678wertyui;en1e3kj3e31jn2algoritmgenerateddata')
});

it('[5] encode with default config with default values', () => {
Expand All @@ -153,7 +151,7 @@ describe('encode uri with configs using default values', () => {
iv: '2345678wertyui',
pad: 'pkcs#7'
}
}).encoded).toEqual('encrypted:aes/cbc?iv=2345678wertyui&pad=pkcs%237;en1e3kj3e31jn2algoritmgenerateddata')
}).encoded).toEqual('encrypted:?iv=2345678wertyui&pad=pkcs%237;en1e3kj3e31jn2algoritmgenerateddata')
});

it('[6] encode with default config with default values', () => {
Expand All @@ -171,7 +169,7 @@ describe('encode uri with configs using default values', () => {
algorithm: 'aes/cbc',
cipher: 'en1e3kj3e31jn2algoritmgenerateddata',
queryString: 'iv=2345678wertyui'
}).encoded).toEqual('encrypted:aes/cbc?iv=2345678wertyui;en1e3kj3e31jn2algoritmgenerateddata')
}).encoded).toEqual('encrypted:?iv=2345678wertyui;en1e3kj3e31jn2algoritmgenerateddata')
});

it('[8] encode with default config with default values', () => {
Expand All @@ -182,7 +180,7 @@ describe('encode uri with configs using default values', () => {
iv: '2345678wertyui',
pad: 'pkcs#7'
}
}).encoded).toEqual('encrypted:aes/cbc?iv=2345678wertyui&pad=pkcs%237;en1e3kj3e31jn2algoritmgenerateddata')
}).encoded).toEqual('encrypted:?iv=2345678wertyui&pad=pkcs%237;en1e3kj3e31jn2algoritmgenerateddata')
});
});

Expand Down Expand Up @@ -244,7 +242,7 @@ describe('uri matcher', () => {
});

it('[3] match valid encrypted uri', () => {
expect(EncryptedURIParser.matcher('encrypted:aes/cbc?2345678wertyui;en1e3kj3e31jn2algoritmgenerateddata'))
expect(EncryptedURIParser.matcher('encrypted:?2345678wertyui;en1e3kj3e31jn2algoritmgenerateddata'))
.toEqual(true);
});

Expand All @@ -259,7 +257,7 @@ describe('uri matcher', () => {
});

it('[6] match valid encrypted uri', () => {
expect(EncryptedURIParser.matcher('encrypted:aes/cbc?iv=2345678wertyui&pad=pkcs%237;en1e3kj3e31jn2algoritmgenerateddata'))
expect(EncryptedURIParser.matcher('encrypted:?iv=2345678wertyui&pad=pkcs%237;en1e3kj3e31jn2algoritmgenerateddata'))
.toEqual(true);
});

Expand Down
32 changes: 24 additions & 8 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ export type TEncryptedURI<T extends TURIParams> = {
};

class EncryptedURIDecoder<T extends TURIParams> {



private readonly ENCRYPTED_URI_MATCHER = /^encrypted:/;
private readonly QUERY_STRING_MATCHER = /^\?[^;]*;/;
Expand Down Expand Up @@ -229,7 +227,7 @@ class EncryptedURIEncoder<T extends TURIParams> {
return params;
}

encode(content: TEncryptedURI<T> & { kdf?: TEncryptedURIKDFParams }): string {
encode(content: TEncryptedURI<T> & { kdf?: TEncryptedURIKDFParams, config?: TEncryptedDefaultsConfig }): string {
const algorithm = this.encodeAlgorithm(content);
const parameters = this.encodeParameters(content);

Expand Down Expand Up @@ -263,8 +261,19 @@ class EncryptedURIEncoder<T extends TURIParams> {
}

private encodeAlgorithm(
content: TEncryptedURI<T>
content: TEncryptedURI<T> & {
config?: TEncryptedDefaultsConfig
}
): string {
const config = EncryptedURI.getConfigsOfDefaults(content.config);

if (
config.ignoreDefaultAlgorithm &&
content.algorithm === EncryptedURI.defaultAlgotithm
) {
return '';
}

return content.algorithm || '';
}
}
Expand All @@ -280,10 +289,12 @@ export class EncryptedURIParser<T extends TURIParams> {

constructor(content: string);
constructor(content: TEncryptedURI<T> & {
kdf?: TEncryptedURIKDFParams | undefined;
kdf?: TEncryptedURIKDFParams;
config?: TEncryptedDefaultsConfig;
});
constructor(content: string | TEncryptedURI<T> & {
kdf?: TEncryptedURIKDFParams | undefined;
kdf?: TEncryptedURIKDFParams;
config?: TEncryptedDefaultsConfig;
}) {
if (typeof content === 'string') {
const decoder = new EncryptedURIDecoder<T>();
Expand Down Expand Up @@ -530,7 +541,8 @@ export class EncryptedURI {
}

static encode<T extends TURIParams>(params: TEncryptedURI<T> & {
kdf?: TEncryptedURIKDFParams | undefined;
kdf?: TEncryptedURIKDFParams;
config?: TEncryptedDefaultsConfig;
}): string {
return new EncryptedURIParser(params).encoded;
}
Expand All @@ -542,7 +554,11 @@ export class EncryptedURI {
const ciphred = await new encrypter(params, ...args).encrypt();
ciphred.algorithm = encrypter.algorithm || params.algorithm;

return Promise.resolve(this.encode({ ...ciphred, kdf: params.kdf }));
return Promise.resolve(this.encode({
...ciphred,
kdf: params.kdf,
config: params.config
}));
}

static decrypt(
Expand Down

0 comments on commit 9a310dc

Please sign in to comment.