Skip to content

Commit

Permalink
chore: code style
Browse files Browse the repository at this point in the history
  • Loading branch information
stnmtz committed Oct 22, 2024
1 parent d3be3fe commit 1b721c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/CryptoDerivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ export class CryptoDerivation implements ICryptoDerivation {
memlimit = 8192
): Promise<CryptoSecretKey> {
const sodium: any = (await SodiumWrapper.ready()) as any;
if (salt.buffer.byteLength !== 16) {
if (salt.buffer.byteLength !== sodium.crypto_pwhash_SALTBYTES) {
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}.`);
}

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}.`);
}

if (memlimit < 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}.`);
}
Expand Down
11 changes: 5 additions & 6 deletions test/crypto/CryptoDerivationTest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
CryptoHash,
CryptoHashAlgorithm,
CryptoSecretKey,
Encoding,
ICoreBuffer
} from "@nmshd/crypto";
import { expect } from "chai";
Expand All @@ -19,7 +18,7 @@ export class CryptoDerivationTest {
before(async function () {
// Create 256bit entropy
keybuffer = await CryptoHash.hash(
CoreBuffer.fromString("test", Encoding.Utf8),
CoreBuffer.fromUtf8("test"),
CryptoHashAlgorithm.SHA256
);
});
Expand Down Expand Up @@ -48,7 +47,7 @@ export class CryptoDerivationTest {
);

const keybuffer2 = await CryptoHash.hash(
CoreBuffer.fromString("test2", Encoding.Utf8),
CoreBuffer.fromUtf8("test2"),
CryptoHashAlgorithm.SHA256
);
const derivedComparison = await CryptoDerivation.deriveKeyFromBase(keybuffer2, 0, "12345678");
Expand Down Expand Up @@ -102,7 +101,7 @@ export class CryptoDerivationTest {
salt = CoreBuffer.from(
(
await CryptoHash.hash(
CoreBuffer.fromString("test", Encoding.Utf8),
CoreBuffer.fromUtf8("test"),
CryptoHashAlgorithm.SHA256
)
).buffer.subarray(0, 16)
Expand Down Expand Up @@ -150,7 +149,7 @@ export class CryptoDerivationTest {
"eyJrZXkiOiI5ck1uY2NOODlsRVpXNVJuQWdpWWk4Tm9xY21vOWIyMmFYQmpuMTlRV0ZRIiwiYWxnIjozLCJAdHlwZSI6IkNyeXB0b1NlY3JldEtleSJ9"
);

const master2 = CoreBuffer.fromString("test2", Encoding.Utf8);
const master2 = CoreBuffer.fromUtf8("test2");
const derivedComparison = await CryptoDerivation.deriveKeyFromPassword(
master2,
salt,
Expand Down Expand Up @@ -182,7 +181,7 @@ export class CryptoDerivationTest {
const salt2 = CoreBuffer.from(
(
await CryptoHash.hash(
CoreBuffer.fromString("test2", Encoding.Utf8),
CoreBuffer.fromUtf8("test2"),
CryptoHashAlgorithm.SHA256
)
).buffer.subarray(0, 16)
Expand Down

0 comments on commit 1b721c3

Please sign in to comment.