Commit 13c0e90b authored by David Burke's avatar David Burke

more

parent 6e83d991
......@@ -15,7 +15,9 @@ export declare class Common {
static crypto_pwhash(keyLength: number, password: string | Uint8Array, salt: Uint8Array, opsLimit: number, memlimit: number, algorithm: number): Uint8Array;
private static force_bytes(input);
private static force_java_bytes(input);
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 readonly crypto_aead_chacha20poly1305_ABYTES: number;
static readonly crypto_aead_chacha20poly1305_KEYBYTES: number;
static readonly crypto_aead_chacha20poly1305_NPUBBYTES: number;
......
......@@ -2,12 +2,14 @@ import { Observable } from 'tns-core-modules/data/observable';
import * as app from 'tns-core-modules/application';
import * as dialogs from 'tns-core-modules/ui/dialogs';
import { TextEncoder } from "text-encoding";
import { TextEncoder, TextDecoder, Buffer } from "text-encoding";
const Random = org.libsodium.jni.crypto.Random;
const SodiumConstants = org.libsodium.jni.SodiumConstants;
const NaCl = org.libsodium.jni.NaCl;
const Sodium = org.libsodium.jni.Sodium;
const Arrays = java.util.Arrays;
NaCl.sodium();
let Base64 = android.util.Base64;
......@@ -101,13 +103,23 @@ export class Common {
return this.Uint8ArrayToJavaByteArray(jsBytes);
}
private static trimJavaBytes(input: JavaBytes): JavaBytes {
let i = input.length - 1;
while (i >= 0 && input[i] === 0) {
--i;
}
let out = Arrays.copyOf(input as any, i + 1);
return (out as any);
}
public static crypto_secretbox_easy(message: string | Uint8Array, nonce: Uint8Array, key: Uint8Array): Uint8Array {
let messageBytes = this.force_java_bytes(message);
let nonceBytes = this.force_java_bytes(nonce);
let keyBytes = this.force_java_bytes(key);
let ciphertext: JavaBytes = Array.create('byte', Sodium.crypto_secretbox_macbytes() + messageBytes.length);
let ciphertext: JavaBytes = Array.create('byte', Sodium.crypto_secretbox_macbytes() + messageBytes.length);
Sodium.crypto_secretbox_easy(
Sodium.crypto_secretbox_easy(
ciphertext,
messageBytes,
messageBytes.length,
......@@ -118,6 +130,25 @@ export class Common {
return this.javaByteArrayToUint8Array(ciphertext);
}
public static crypto_secretbox_open_easy(ciphertext: Uint8Array, nonce: Uint8Array, key: Uint8Array): Uint8Array {
let ciphertextBytes = this.force_java_bytes(ciphertext);
let nonceBytes = this.force_java_bytes(nonce);
let keyBytes = this.force_java_bytes(key);
let decrypted: JavaBytes = Array.create('byte', Sodium.crypto_secretbox_macbytes() + ciphertextBytes.length);
Sodium.crypto_secretbox_open_easy(
decrypted,
ciphertextBytes,
ciphertextBytes.length,
nonceBytes,
keyBytes,
);
decrypted = this.trimJavaBytes(decrypted);
return this.javaByteArrayToUint8Array(decrypted);
}
public static get crypto_aead_chacha20poly1305_ABYTES() { return Sodium.crypto_aead_chacha20poly1305_abytes(); }
public static get crypto_aead_chacha20poly1305_KEYBYTES() { return Sodium.crypto_aead_chacha20poly1305_keybytes(); }
public static get crypto_aead_chacha20poly1305_NPUBBYTES() { return Sodium.crypto_aead_chacha20poly1305_npubbytes(); }
......@@ -200,6 +231,15 @@ export class Common {
);
msg += `The hash is ${hash}\n`;
let secretMessage = 'helloo';
let nonce = Buffer.from(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]);
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);
msg += `decrypted is ${decryptedString}\n`;
return msg;
}
}
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