Skip to content

Commit

Permalink
atualizando documentação
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioconselheiro committed Feb 14, 2024
1 parent 9fb022f commit b7904b5
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Algorithm with no param:
Basic use, how to decode and encrypt and how to decode and decrypt:

```typescript
import { EncryptedURIAlgorithm, EncryptedURIDecrypter, EncryptedURIEncrypter, TEncryptedURI, TEncryptedURIKDFConfig, TEncryptedURIResultset, TURIParams } from '@encrypted-uri/core';
import { EncryptedURIAlgorithm, EncryptedURIDecrypter, EncryptedURIEncrypter, TEncryptedURI, TEncryptedURIResultset, TURIParams } from '@encrypted-uri/core';
import { ecb } from '@noble/ciphers/aes';
import { bytesToUtf8, utf8ToBytes } from '@noble/ciphers/utils';
import { randomBytes } from '@noble/hashes/utils';
Expand All @@ -70,10 +70,9 @@ import { base64 } from '@scure/base';
class EncryptedURIAESECBDecrypter<T extends TURIParams = TURIParams> extends EncryptedURIDecrypter<T> {
constructor(
decoded: TEncryptedURI<T>,
password: string,
defaultsKDF: Required<TEncryptedURIKDFConfig>
password: string
) {
super(decoded, password, defaultsKDF);
super(decoded, password);
}

async decrypt(): Promise<string> {
Expand All @@ -90,10 +89,11 @@ class EncryptedURIAESECBDecrypter<T extends TURIParams = TURIParams> extends Enc
algorithm: 'aes/ecb',
decrypter: EncryptedURIAESECBDecrypter
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class EncryptedURIAESECBEncrypter<T extends TURIParams = TURIParams> extends EncryptedURIEncrypter<TURIParams> {

constructor(
protected override params: TEncryptedURIResultset<T>
params: TEncryptedURIResultset<T>
) {
super(params);
}
Expand All @@ -102,18 +102,19 @@ 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.kdf)).encrypt(content);
const rawCipher = await ecb(kdf(this.params.password, salt, this.params)).encrypt(content);
const cipher = base64.encode(OpenSSLSerializer.encode(rawCipher, salt));

return Promise.resolve({ cipher });
}
}


```

Advanced use, how to add default encrypter and how to add more alias to an algorithm:
```typescript
import { EncryptedURI, EncryptedURIAlgorithm, EncryptedURIDecrypter, EncryptedURIEncrypter, TEncryptedURI, TEncryptedURIKDFConfig, TEncryptedURIResultset } from '@encrypted-uri/core';
import { EncryptedURI, EncryptedURIAlgorithm, EncryptedURIDecrypter, EncryptedURIEncrypter, TEncryptedURI, TEncryptedURIResultset } from '@encrypted-uri/core';
import { bytesToUtf8, hexToBytes, utf8ToBytes } from '@noble/ciphers/utils';
import { cbc } from '@noble/ciphers/webcrypto/aes';
import { randomBytes } from '@noble/hashes/utils';
Expand All @@ -122,10 +123,9 @@ import { base64 } from '@scure/base';
class EncryptedURIAESCBCDecrypter extends EncryptedURIDecrypter<TInitializationVectorParams> {
constructor(
decoded: TEncryptedURI<TInitializationVectorParams>,
password: string,
defaultsKDF: Required<TEncryptedURIKDFConfig>
password: string
) {
super(decoded, password, defaultsKDF);
super(decoded, password);
}

async decrypt(): Promise<string> {
Expand All @@ -147,7 +147,7 @@ class EncryptedURIAESCBCDecrypter extends EncryptedURIDecrypter<TInitializationV
class EncryptedURIAESCBCEncrypter extends EncryptedURIEncrypter<TInitializationVectorParams> {

constructor(
protected override params: TEncryptedURIResultset<TInitializationVectorParams>
params: TEncryptedURIResultset<TInitializationVectorParams>
) {
super(params);
}
Expand All @@ -158,7 +158,7 @@ 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.kdf), iv).encrypt(content);
const cipher = await cbc(kdf(this.params.password, salt, this.params), iv).encrypt(content);

return Promise.resolve({
cipher: base64.encode(OpenSSLSerializer.encode(cipher, salt)),
Expand All @@ -169,6 +169,7 @@ class EncryptedURIAESCBCEncrypter extends EncryptedURIEncrypter<TInitializationV

EncryptedURI.setAlgorithm('', EncryptedURIAESCBCEncrypter, EncryptedURIAESCBCDecrypter);
EncryptedURI.setAlgorithm('aes', EncryptedURIAESCBCEncrypter, EncryptedURIAESCBCDecrypter);

```

## Example of practical application
Expand Down

0 comments on commit b7904b5

Please sign in to comment.