Commit 8b89df02 authored by David Burke's avatar David Burke

crypto_box_seal and crypto_box_seal_open now work and have tests

parent 2e520fc6
......@@ -30,4 +30,13 @@ describe("libsodium nativescript wrapper", function() {
let decryptedString = new TextDecoder('utf-8').decode(decrypted);
expect(decryptedString).toEqual(secretMessage);
});
it("can run crypto_box_seal and crypto_box_seal_open", function() {
let secretMessage = 'hello';
let keypair = Libsodium.crypto_box_keypair();
let ciphertext = Libsodium.crypto_box_seal(secretMessage, keypair.publicKey);
let decrypted = Libsodium.crypto_box_seal_open(ciphertext, keypair.publicKey, keypair.privateKey);
let decryptedString = new TextDecoder('utf-8').decode(decrypted);
expect(decryptedString).toEqual(secretMessage);
});
});
......@@ -18,6 +18,8 @@ export declare class Common {
private static trimJavaBytes(input);
static crypto_secretbox_easy(message: string | Uint8Array, nonce: Uint8Array, key: Uint8Array): Uint8Array;
static crypto_secretbox_open_easy(ciphertext: Uint8Array, nonce: Uint8Array, key: Uint8Array): Uint8Array;
static crypto_box_seal(message: string | Uint8Array, publicKey: Uint8Array): Uint8Array;
static crypto_box_seal_open(ciphertext: Uint8Array, publicKey: Uint8Array, secretKey: Uint8Array): Uint8Array;
static readonly crypto_aead_chacha20poly1305_ABYTES: number;
static readonly crypto_aead_chacha20poly1305_KEYBYTES: number;
static readonly crypto_aead_chacha20poly1305_NPUBBYTES: number;
......
......@@ -145,7 +145,28 @@ export class Common {
);
decrypted = this.trimJavaBytes(decrypted);
return this.javaByteArrayToUint8Array(decrypted);
}
public static crypto_box_seal(message: string | Uint8Array, publicKey: Uint8Array): Uint8Array {
let messageBytes = this.force_java_bytes(message);
let publicKeyBytes = this.force_java_bytes(publicKey);
let ciphertext: JavaBytes = Array.create('byte', Sodium.crypto_box_sealbytes() + messageBytes.length);
Sodium.crypto_box_seal(ciphertext, messageBytes, messageBytes.length, publicKeyBytes);
return this.javaByteArrayToUint8Array(ciphertext);
}
public static crypto_box_seal_open(ciphertext: Uint8Array, publicKey: Uint8Array, secretKey: Uint8Array): Uint8Array {
let ciphertextBytes = this.force_java_bytes(ciphertext);
let publicKeyBytes = this.force_java_bytes(publicKey);
let secretKeyBytes = this.force_java_bytes(secretKey);
let decrypted: JavaBytes = Array.create('byte', Sodium.crypto_box_sealbytes() + ciphertextBytes.length);
Sodium.crypto_box_seal_open(decrypted, ciphertextBytes, ciphertextBytes.length, publicKeyBytes, secretKeyBytes);
decrypted = this.trimJavaBytes(decrypted);
return this.javaByteArrayToUint8Array(decrypted);
}
......@@ -231,11 +252,15 @@ export class Common {
);
msg += `The hash is ${hash}\n`;
// let secretMessage = 'hello';
// let nonce = this.randombytes_buf(this.crypto_secretbox_NONCEBYTES);
// let key = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5]);
// let ciphertext = this.crypto_secretbox_easy(secretMessage, nonce, key);
// let decrypted = this.crypto_secretbox_open_easy(ciphertext, nonce, key);
// let decryptedString = new TextDecoder('utf-8').decode(decrypted);
let secretMessage = 'hello';
let nonce = this.randombytes_buf(this.crypto_secretbox_NONCEBYTES);
let key = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5]);
let ciphertext = this.crypto_secretbox_easy(secretMessage, nonce, key);
let decrypted = this.crypto_secretbox_open_easy(ciphertext, nonce, key);
let ciphertext = this.crypto_box_seal(secretMessage, keypair.publicKey);
let decrypted = this.crypto_box_seal_open(ciphertext, keypair.publicKey, keypair.privateKey);
let decryptedString = new TextDecoder('utf-8').decode(decrypted);
msg += `decrypted is ${decryptedString}\n`;
......
......@@ -30,8 +30,8 @@
"Android"
],
"author": {
"name": "Your Name",
"email": "youremail@yourdomain.com"
"name": "David Burke",
"email": "david@burkesoftware.com"
},
"bugs": {
"url": "https://gitlab.com/burke-software/nativescript-libsodium/issues"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment