Skip to content

Commit

Permalink
Improve wasm-themis to be more compatible with TypeScript (#909)
Browse files Browse the repository at this point in the history
* Added second optional parameter to seal encrypt/decrypt

* Remove single deprecated invalid parameter in single test case

* increase minor subversion

* update packages

* use suggested fixes

* use suggested fixes

* update package-lock.json

* restore package.json and package-lock.json files with npm audit fix
  • Loading branch information
radetsky committed Apr 14, 2022
1 parent 2667a99 commit 67732ab
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 57 deletions.
76 changes: 38 additions & 38 deletions src/wrappers/themis/wasm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/wrappers/themis/wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wasm-themis",
"version": "0.14.0",
"version": "0.14.5",
"description": "Themis is a convenient cryptographic library for data protection.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
34 changes: 17 additions & 17 deletions src/wrappers/themis/wasm/src/secure_cell_seal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class SecureCellSeal {
return new SecureCellSealWithPassphrase(passphrase);
}

encrypt(message: Uint8Array) {
encrypt(message: Uint8Array, context: Uint8Array = new Uint8Array()) {
message = coerceToBytes(message);
if (message.length == 0) {
throw new ThemisError(
Expand All @@ -83,12 +83,10 @@ export class SecureCellSeal {
"message must be not empty"
);
}

let context;
if (arguments.length > 1 && arguments[1] !== null) {
context = coerceToBytes(arguments[1]);
} else {
if (context === null || context === undefined) {
context = new Uint8Array();
} else {
context = coerceToBytes(context);
}

let status;
Expand Down Expand Up @@ -171,7 +169,11 @@ export class SecureCellSeal {
);
}

context = coerceToBytes(context);
if (context === null || context === undefined) {
context = new Uint8Array();
} else {
context = coerceToBytes(context);
}

let status;
/// C API uses "size_t" for lengths, it's defined as "i32" in Emscripten
Expand Down Expand Up @@ -249,7 +251,7 @@ class SecureCellSealWithPassphrase extends SecureCellSeal {
super(passphraseBytes(passphrase));
}

encrypt(message: Uint8Array) {
encrypt(message: Uint8Array, context: Uint8Array = new Uint8Array()) {
message = coerceToBytes(message);
if (message.length == 0) {
throw new ThemisError(
Expand All @@ -259,11 +261,10 @@ class SecureCellSealWithPassphrase extends SecureCellSeal {
);
}

let context;
if (arguments.length > 1 && arguments[1] !== null) {
context = coerceToBytes(arguments[1]);
} else {
if (context === null || context === undefined) {
context = new Uint8Array();
} else {
context = coerceToBytes(context);
}

let status;
Expand Down Expand Up @@ -336,7 +337,7 @@ class SecureCellSealWithPassphrase extends SecureCellSeal {
}
}

decrypt(message: Uint8Array) {
decrypt(message: Uint8Array, context: Uint8Array = new Uint8Array()) {
message = coerceToBytes(message);
if (message.length == 0) {
throw new ThemisError(
Expand All @@ -346,11 +347,10 @@ class SecureCellSealWithPassphrase extends SecureCellSeal {
);
}

let context;
if (arguments.length > 1 && arguments[1] !== null) {
context = coerceToBytes(arguments[1]);
} else {
if (context === null || context === undefined) {
context = new Uint8Array();
} else {
context = coerceToBytes(context);
}

let status;
Expand Down
26 changes: 25 additions & 1 deletion src/wrappers/themis/wasm/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ describe('wasm-themis', function() {
let decrypted = cell.decrypt(encrypted)
assert.deepStrictEqual(decrypted, testInput)
})
it('undefined context == no context', function() {
let cell = themis.SecureCellSeal.withKey(masterKey1)
let encrypted = cell.encrypt(testInput, undefined)
let decrypted = cell.decrypt(encrypted, undefined)
assert.deepStrictEqual(decrypted, testInput)
})
it('omitted context == no context', function() {
let cell = themis.SecureCellSeal.withKey(masterKey1)
let encrypted = cell.encrypt(testInput)
let decrypted = cell.decrypt(encrypted)
assert.deepStrictEqual(decrypted, testInput)
})
it('detects invalid master key', function() {
let cell1 = themis.SecureCellSeal.withKey(masterKey1)
let cell2 = themis.SecureCellSeal.withKey(masterKey2)
Expand Down Expand Up @@ -297,6 +309,18 @@ describe('wasm-themis', function() {
let decrypted = cell.decrypt(encrypted)
assert.deepStrictEqual(decrypted, testInput)
})
it('undefined context == no context', function() {
let cell = themis.SecureCellSeal.withPassphrase(passphrase1)
let encrypted = cell.encrypt(testInput, undefined)
let decrypted = cell.decrypt(encrypted, undefined)
assert.deepStrictEqual(decrypted, testInput)
})
it('omitted context == no context', function() {
let cell = themis.SecureCellSeal.withPassphrase(passphrase1)
let encrypted = cell.encrypt(testInput)
let decrypted = cell.decrypt(encrypted)
assert.deepStrictEqual(decrypted, testInput)
})
it('detects invalid passphrase', function() {
let cell1 = themis.SecureCellSeal.withPassphrase(passphrase1)
let cell2 = themis.SecureCellSeal.withPassphrase(passphrase2)
Expand Down Expand Up @@ -349,7 +373,7 @@ describe('wasm-themis', function() {
assert.throws(() => cell.encrypt(invalid), TypeError)
assert.throws(() => cell.decrypt(invalid), TypeError)
// null context is okay, it should not throw
if (invalid !== null) {
if (invalid !== null && invalid !== undefined) {
assert.throws(() => cell.encrypt(testInput, invalid), TypeError)
assert.throws(() => cell.decrypt(encrypted, invalid), TypeError)
}
Expand Down

0 comments on commit 67732ab

Please sign in to comment.