Skip to content

Commit

Permalink
chore: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
stnmtz committed Oct 21, 2024
1 parent 665b1b3 commit 75cecb3
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 94 deletions.
5 changes: 2 additions & 3 deletions README_dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ Afterwards, open your browser and enter

To start the test, type "mocha.run()" into the browser's console.


# Updates

ts-crypto so far depends on following packages, which cannot be updated:

- @js-soft/ts-serval 2.0.11 does not export lib-web libraries
- chai ^5.1.1 results in an *.ts error
- @js-soft/ts-serval 2.0.11 does not export lib-web libraries
- chai ^5.1.1 results in an \*.ts error
27 changes: 16 additions & 11 deletions src/CryptoDerivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface ICryptoDerivationStatic {
masterKey: ICoreBuffer,
salt: ICoreBuffer,
keyAlgorithm: CryptoEncryptionAlgorithm,
derivationAlgorithm:CryptoDerivationAlgorithm,
derivationAlgorithm: CryptoDerivationAlgorithm,
iterations: number,
memlimit: number
): Promise<CryptoSecretKey>;
Expand All @@ -37,29 +37,27 @@ export class CryptoDerivation implements ICryptoDerivation {
password: ICoreBuffer,
salt: ICoreBuffer,
keyAlgorithm: CryptoEncryptionAlgorithm = CryptoEncryptionAlgorithm.XCHACHA20_POLY1305,
derivationAlgorithm:CryptoDerivationAlgorithm = CryptoDerivationAlgorithm.ARGON2ID,
derivationAlgorithm: CryptoDerivationAlgorithm = CryptoDerivationAlgorithm.ARGON2ID,
opslimit: number = 100000,
memlimit: number = 8192
): Promise<CryptoSecretKey> {
const sodium:any = await SodiumWrapper.ready() as any
const sodium: any = (await SodiumWrapper.ready()) as any;
if (salt.buffer.byteLength !== 16) {
throw new Error(`The salt must be exactly ${sodium.crypto_pwhash_SALTBYTES} bytes long!`);
}
if (opslimit < sodium.crypto_pwhash_OPSLIMIT_MIN) {
throw new Error(`The opslimit must be higher than ${sodium.crypto_pwhash_OPSLIMIT_MIN}.`)
throw new Error(`The opslimit must be higher than ${sodium.crypto_pwhash_OPSLIMIT_MIN}.`);
}
if (sodium.crypto_pwhash_OPSLIMIT_MAX > 0 && opslimit > sodium.crypto_pwhash_OPSLIMIT_MAX) {
throw new Error(`The opslimit must be lower than ${sodium.crypto_pwhash_OPSLIMIT_MAX}.`)
throw new Error(`The opslimit must be lower than ${sodium.crypto_pwhash_OPSLIMIT_MAX}.`);
}
if (memlimit < sodium.crypto_pwhash_MEMLIMIT_MIN) {
throw new Error(`The memlimit must be higher than ${sodium.crypto_pwhash_MEMLIMIT_MIN}.`)
throw new Error(`The memlimit must be higher than ${sodium.crypto_pwhash_MEMLIMIT_MIN}.`);
}
if (sodium.crypto_pwhash_MEMLIMIT_MAX > 0 && memlimit > sodium.crypto_pwhash_MEMLIMIT_MAX) {
throw new Error(`The memlimit must be lower than ${sodium.crypto_pwhash_MEMLIMIT_MAX}.`)
throw new Error(`The memlimit must be lower than ${sodium.crypto_pwhash_MEMLIMIT_MAX}.`);
}



let keyLength;
switch (keyAlgorithm) {
case CryptoEncryptionAlgorithm.AES128_GCM:
Expand All @@ -73,7 +71,7 @@ export class CryptoDerivation implements ICryptoDerivation {
throw new Error("KeyAlgorithm not supported.");
}

let derivationAlgorithmAsNumber:number;
let derivationAlgorithmAsNumber: number;
switch (derivationAlgorithm) {
case CryptoDerivationAlgorithm.ARGON2I:
derivationAlgorithmAsNumber = 1;
Expand All @@ -85,7 +83,14 @@ export class CryptoDerivation implements ICryptoDerivation {
throw new Error("DerivationAlgorithm not supported.");
}

const pwhash = (await SodiumWrapper.ready()).crypto_pwhash(keyLength, password.buffer, salt.buffer, opslimit, memlimit, derivationAlgorithmAsNumber);
const pwhash = (await SodiumWrapper.ready()).crypto_pwhash(
keyLength,
password.buffer,
salt.buffer,
opslimit,
memlimit,
derivationAlgorithmAsNumber
);
const hashBuffer = CoreBuffer.from(pwhash);
return CryptoSecretKey.from({ secretKey: hashBuffer, algorithm: keyAlgorithm });
}
Expand Down
Loading

0 comments on commit 75cecb3

Please sign in to comment.