Skip to content

Commit

Permalink
made TS config strict and fixed some strict errors
Browse files Browse the repository at this point in the history
  • Loading branch information
brainfoolong committed Nov 25, 2023
1 parent 9348e14 commit fd01e0f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
9 changes: 6 additions & 3 deletions dist/ascon.es6.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// js-ascon v1.0.0 @ https://github.com/brainfoolong/js-ascon
// js-ascon v1.0.2 @ https://github.com/brainfoolong/js-ascon
/**
* Javascript / Typescript implementation of Ascon v1.2
* Heavily inspired by the python implementation of https://github.com/meichlseder/pyascon
Expand Down Expand Up @@ -31,7 +31,7 @@ export default class JsAscon {
*/
static decryptFromHex(secretKey, hexStr, associatedData = null, cipherVariant = 'Ascon-128') {
const key = JsAscon.hash(secretKey, 'Ascon-Xof', cipherVariant === 'Ascon-80pq' ? 20 : 16);
const hexData = Uint8Array.from(hexStr.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
const hexData = Uint8Array.from((hexStr.match(/.{1,2}/g) || []).map((byte) => parseInt(byte, 16)));
const plaintextMessage = JsAscon.decrypt(key, hexData.slice(-16), associatedData !== null ? JSON.stringify(associatedData) : '', hexData.slice(0, -16), cipherVariant);
return plaintextMessage !== null ? JSON.parse(JsAscon.byteArrayToStr(plaintextMessage)) : null;
}
Expand Down Expand Up @@ -152,6 +152,7 @@ export default class JsAscon {
let hash = [];
JsAscon.permutation(data, permutationRoundsA);
while (hash.length < hashLength) {
// @ts-ignore
hash = hash.concat(...JsAscon.bigIntToByteArray(data[0]));
JsAscon.permutation(data, permutationRoundsB);
}
Expand Down Expand Up @@ -225,6 +226,7 @@ export default class JsAscon {
let tag = [];
JsAscon.permutation(data, permutationRoundsA);
while (tag.length < tagLength) {
// @ts-ignore
tag = tag.concat(...JsAscon.bigIntToByteArray(data[0]), ...JsAscon.bigIntToByteArray(data[1]));
JsAscon.permutation(data, permutationRoundsB);
}
Expand Down Expand Up @@ -407,7 +409,7 @@ export default class JsAscon {
data[0] ^= data[4];
data[4] ^= data[3];
data[2] ^= data[1];
let t = [];
let t = new Array();
for (let i = 0; i <= 4; i++) {
t[i] = (data[i] ^ BigInt('0xffffffffffffffff')) & data[(i + 1) % 5];
}
Expand Down Expand Up @@ -585,6 +587,7 @@ export default class JsAscon {
// @ts-ignore
return JsAscon.anyToByteArray(crypto.randomBytes(length));
}
return new Uint8Array(0);
}
/**
* Debug output
Expand Down
9 changes: 6 additions & 3 deletions dist/ascon.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// js-ascon v1.0.0 @ https://github.com/brainfoolong/js-ascon
// js-ascon v1.0.2 @ https://github.com/brainfoolong/js-ascon
/**
* Javascript / Typescript implementation of Ascon v1.2
* Heavily inspired by the python implementation of https://github.com/meichlseder/pyascon
Expand Down Expand Up @@ -31,7 +31,7 @@ class JsAscon {
*/
static decryptFromHex(secretKey, hexStr, associatedData = null, cipherVariant = 'Ascon-128') {
const key = JsAscon.hash(secretKey, 'Ascon-Xof', cipherVariant === 'Ascon-80pq' ? 20 : 16);
const hexData = Uint8Array.from(hexStr.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
const hexData = Uint8Array.from((hexStr.match(/.{1,2}/g) || []).map((byte) => parseInt(byte, 16)));
const plaintextMessage = JsAscon.decrypt(key, hexData.slice(-16), associatedData !== null ? JSON.stringify(associatedData) : '', hexData.slice(0, -16), cipherVariant);
return plaintextMessage !== null ? JSON.parse(JsAscon.byteArrayToStr(plaintextMessage)) : null;
}
Expand Down Expand Up @@ -152,6 +152,7 @@ class JsAscon {
let hash = [];
JsAscon.permutation(data, permutationRoundsA);
while (hash.length < hashLength) {
// @ts-ignore
hash = hash.concat(...JsAscon.bigIntToByteArray(data[0]));
JsAscon.permutation(data, permutationRoundsB);
}
Expand Down Expand Up @@ -225,6 +226,7 @@ class JsAscon {
let tag = [];
JsAscon.permutation(data, permutationRoundsA);
while (tag.length < tagLength) {
// @ts-ignore
tag = tag.concat(...JsAscon.bigIntToByteArray(data[0]), ...JsAscon.bigIntToByteArray(data[1]));
JsAscon.permutation(data, permutationRoundsB);
}
Expand Down Expand Up @@ -407,7 +409,7 @@ class JsAscon {
data[0] ^= data[4];
data[4] ^= data[3];
data[2] ^= data[1];
let t = [];
let t = new Array();
for (let i = 0; i <= 4; i++) {
t[i] = (data[i] ^ BigInt('0xffffffffffffffff')) & data[(i + 1) % 5];
}
Expand Down Expand Up @@ -585,6 +587,7 @@ class JsAscon {
// @ts-ignore
return JsAscon.anyToByteArray(crypto.randomBytes(length));
}
return new Uint8Array(0);
}
/**
* Debug output
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-ascon",
"version": "1.0.1",
"version": "1.0.2",
"description": "JavaScript / TypeScript Implementation of Ascon, a family of authenticated encryption (AEAD) and hashing algorithms designed to be lightweight.",
"scripts": {
"dist": "node ./node_modules/typescript/bin/tsc --project tsconfig.json && node build/dist.js",
Expand Down
7 changes: 5 additions & 2 deletions src/ascon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class JsAscon {
cipherVariant: string = 'Ascon-128'
): any {
const key = JsAscon.hash(secretKey, 'Ascon-Xof', cipherVariant === 'Ascon-80pq' ? 20 : 16)
const hexData = Uint8Array.from(hexStr.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)))
const hexData = Uint8Array.from((hexStr.match(/.{1,2}/g) || []).map((byte: string) => parseInt(byte, 16)))
const plaintextMessage = JsAscon.decrypt(
key,
hexData.slice(-16),
Expand Down Expand Up @@ -201,6 +201,7 @@ export default class JsAscon {
let hash = []
JsAscon.permutation(data, permutationRoundsA)
while (hash.length < hashLength) {
// @ts-ignore
hash = hash.concat(...JsAscon.bigIntToByteArray(data[0]))
JsAscon.permutation(data, permutationRoundsB)
}
Expand Down Expand Up @@ -295,6 +296,7 @@ export default class JsAscon {
let tag = []
JsAscon.permutation(data, permutationRoundsA)
while (tag.length < tagLength) {
// @ts-ignore
tag = tag.concat(...JsAscon.bigIntToByteArray(data[0]), ...JsAscon.bigIntToByteArray(data[1]))
JsAscon.permutation(data, permutationRoundsB)
}
Expand Down Expand Up @@ -542,7 +544,7 @@ export default class JsAscon {
data[0] ^= data[4]
data[4] ^= data[3]
data[2] ^= data[1]
let t = []
let t = new Array<bigint>()
for (let i = 0; i <= 4; i++) {
t[i] = (data[i] ^ BigInt('0xffffffffffffffff')) & data[(i + 1) % 5]
}
Expand Down Expand Up @@ -740,6 +742,7 @@ export default class JsAscon {
// @ts-ignore
return JsAscon.anyToByteArray(crypto.randomBytes(length))
}
return new Uint8Array(0)
}

/**
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"esModuleInterop": true,
"moduleResolution" : "node",
"target": "ES2020",
"strict": true,
"lib": [
"ES2020",
"DOM"
Expand Down

0 comments on commit fd01e0f

Please sign in to comment.