Commit 6e83d991 authored by David Burke's avatar David Burke

added crypto_secretbox_easy

parent 3153d895
......@@ -13,9 +13,62 @@ export declare class Common {
static randombytes_buf(length: number): Uint8Array;
static crypto_box_keypair(): IKeyPair;
static crypto_pwhash(keyLength: number, password: string | Uint8Array, salt: Uint8Array, opsLimit: number, memlimit: number, algorithm: number): Uint8Array;
static readonly crypto_pwhash_OPSLIMIT_INTERACTIVE: number;
static readonly crypto_pwhash_MEMLIMIT_INTERACTIVE: number;
static readonly crypto_pwhash_ALG_DEFAULT: number;
private static force_bytes(input);
private static force_java_bytes(input);
static crypto_secretbox_easy(message: string | 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;
static readonly crypto_aead_chacha20poly1305_NSECBYTES: number;
static readonly crypto_auth_BYTES: number;
static readonly crypto_auth_KEYBYTES: number;
static readonly crypto_auth_hmacsha256_BYTES: number;
static readonly crypto_auth_hmacsha256_KEYBYTES: number;
static readonly crypto_auth_hmacsha512_BYTES: number;
static readonly crypto_auth_hmacsha512_KEYBYTES: number;
static readonly crypto_box_BEFORENMBYTES: number;
static readonly crypto_box_MACBYTES: number;
static readonly crypto_box_NONCEBYTES: number;
static readonly crypto_box_PUBLICKEYBYTES: number;
static readonly crypto_box_SEALBYTES: number;
static readonly crypto_box_SECRETKEYBYTES: number;
static readonly crypto_box_SEEDBYTES: number;
static readonly crypto_generichash_BYTES: number;
static readonly crypto_generichash_BYTES_MAX: number;
static readonly crypto_generichash_BYTES_MIN: number;
static readonly crypto_generichash_KEYBYTES: number;
static readonly crypto_generichash_KEYBYTES_MAX: number;
static readonly crypto_generichash_KEYBYTES_MIN: number;
static readonly crypto_onetimeauth_BYTES: number;
static readonly crypto_onetimeauth_KEYBYTES: number;
static readonly crypto_pwhash_ALG_ARGON2I13: number;
static readonly crypto_pwhash_ALG_DEFAULT: number;
static readonly crypto_pwhash_MEMLIMIT_INTERACTIVE: number;
static readonly crypto_pwhash_MEMLIMIT_MODERATE: number;
static readonly crypto_pwhash_MEMLIMIT_SENSITIVE: number;
static readonly crypto_pwhash_OPSLIMIT_INTERACTIVE: number;
static readonly crypto_pwhash_OPSLIMIT_MODERATE: number;
static readonly crypto_pwhash_OPSLIMIT_SENSITIVE: number;
static readonly crypto_pwhash_SALTBYTES: number;
static readonly crypto_pwhash_STRBYTES: number;
static readonly crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE: number;
static readonly crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE: number;
static readonly crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE: number;
static readonly crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE: number;
static readonly crypto_pwhash_scryptsalsa208sha256_SALTBYTES: number;
static readonly crypto_pwhash_scryptsalsa208sha256_STRBYTES: number;
static readonly crypto_scalarmult_BYTES: number;
static readonly crypto_scalarmult_SCALARBYTES: number;
static readonly crypto_secretbox_KEYBYTES: number;
static readonly crypto_secretbox_MACBYTES: number;
static readonly crypto_secretbox_NONCEBYTES: number;
static readonly crypto_shorthash_BYTES: number;
static readonly crypto_shorthash_KEYBYTES: number;
static readonly crypto_sign_BYTES: number;
static readonly crypto_sign_PUBLICKEYBYTES: number;
static readonly crypto_sign_SECRETKEYBYTES: number;
static readonly crypto_sign_SEEDBYTES: number;
static readonly crypto_stream_chacha20_KEYBYTES: number;
static readonly crypto_stream_chacha20_NONCEBYTES: number;
static DEMO_MSG(): string;
}
......@@ -65,12 +65,7 @@ export class Common {
}
public static crypto_pwhash(keyLength: number, password: string | Uint8Array, salt: Uint8Array, opsLimit: number, memlimit: number, algorithm: number) {
let passwd: Uint8Array;
if (typeof(password) === "string") {
passwd = new TextEncoder('utf-8').encode(password);
} else {
passwd = password;
}
let passwd = this.force_bytes(password);
let key: JavaBytes = Array.create('byte', keyLength);
let passwordBytes: JavaBytes = this.Uint8ArrayToJavaByteArray(passwd);
......@@ -89,10 +84,95 @@ export class Common {
return keyBytes;
}
public static get crypto_pwhash_OPSLIMIT_INTERACTIVE() { return Sodium.crypto_pwhash_opslimit_interactive(); }
public static get crypto_pwhash_MEMLIMIT_INTERACTIVE() { return Sodium.crypto_pwhash_memlimit_interactive(); }
public static get crypto_pwhash_ALG_DEFAULT() { return Sodium.crypto_pwhash_alg_default(); }
/** Ensure input is Uint8Array, if it's a string, assume it's utf-8 and convert */
private static force_bytes(input: string | Uint8Array) {
let inputBytes: Uint8Array;
if (typeof(input) === "string") {
inputBytes = new TextEncoder('utf-8').encode(input);
} else {
inputBytes = input;
}
return inputBytes;
}
/** Forces input as Java Byte Array */
private static force_java_bytes(input: string | Uint8Array): JavaBytes {
let jsBytes = this.force_bytes(input);
return this.Uint8ArrayToJavaByteArray(jsBytes);
}
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);
Sodium.crypto_secretbox_easy(
ciphertext,
messageBytes,
messageBytes.length,
nonceBytes,
keyBytes,
);
return this.javaByteArrayToUint8Array(ciphertext);
}
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(); }
public static get crypto_aead_chacha20poly1305_NSECBYTES() { return Sodium.crypto_aead_chacha20poly1305_nsecbytes(); }
public static get crypto_auth_BYTES() { return Sodium.crypto_auth_bytes(); }
public static get crypto_auth_KEYBYTES() { return Sodium.crypto_auth_keybytes(); }
public static get crypto_auth_hmacsha256_BYTES() { return Sodium.crypto_auth_hmacsha256_bytes(); }
public static get crypto_auth_hmacsha256_KEYBYTES() { return Sodium.crypto_auth_hmacsha256_keybytes(); }
public static get crypto_auth_hmacsha512_BYTES() { return Sodium.crypto_auth_hmacsha512_bytes(); }
public static get crypto_auth_hmacsha512_KEYBYTES() { return Sodium.crypto_auth_hmacsha512_keybytes(); }
public static get crypto_box_BEFORENMBYTES() { return Sodium.crypto_box_beforenmbytes(); }
public static get crypto_box_MACBYTES() { return Sodium.crypto_box_macbytes(); }
public static get crypto_box_NONCEBYTES() { return Sodium.crypto_box_noncebytes(); }
public static get crypto_box_PUBLICKEYBYTES() { return Sodium.crypto_box_publickeybytes(); }
public static get crypto_box_SEALBYTES() { return Sodium.crypto_box_sealbytes(); }
public static get crypto_box_SECRETKEYBYTES() { return Sodium.crypto_box_secretkeybytes(); }
public static get crypto_box_SEEDBYTES() { return Sodium.crypto_box_seedbytes(); }
public static get crypto_generichash_BYTES() { return Sodium.crypto_generichash_bytes(); }
public static get crypto_generichash_BYTES_MAX() { return Sodium.crypto_generichash_bytes_max(); }
public static get crypto_generichash_BYTES_MIN() { return Sodium.crypto_generichash_bytes_min(); }
public static get crypto_generichash_KEYBYTES() { return Sodium.crypto_generichash_keybytes(); }
public static get crypto_generichash_KEYBYTES_MAX() { return Sodium.crypto_generichash_keybytes_max(); }
public static get crypto_generichash_KEYBYTES_MIN() { return Sodium.crypto_generichash_keybytes_min(); }
public static get crypto_onetimeauth_BYTES() { return Sodium.crypto_onetimeauth_bytes(); }
public static get crypto_onetimeauth_KEYBYTES() { return Sodium.crypto_onetimeauth_keybytes(); }
public static get crypto_pwhash_ALG_ARGON2I13() { return Sodium.crypto_pwhash_alg_argon2i13(); }
public static get crypto_pwhash_ALG_DEFAULT() { return Sodium.crypto_pwhash_alg_default(); }
public static get crypto_pwhash_MEMLIMIT_INTERACTIVE() { return Sodium.crypto_pwhash_memlimit_interactive(); }
public static get crypto_pwhash_MEMLIMIT_MODERATE() { return Sodium.crypto_pwhash_memlimit_moderate(); }
public static get crypto_pwhash_MEMLIMIT_SENSITIVE() { return Sodium.crypto_pwhash_memlimit_sensitive(); }
public static get crypto_pwhash_OPSLIMIT_INTERACTIVE() { return Sodium.crypto_pwhash_opslimit_interactive(); }
public static get crypto_pwhash_OPSLIMIT_MODERATE() { return Sodium.crypto_pwhash_opslimit_moderate(); }
public static get crypto_pwhash_OPSLIMIT_SENSITIVE() { return Sodium.crypto_pwhash_opslimit_sensitive(); }
public static get crypto_pwhash_SALTBYTES() { return Sodium.crypto_pwhash_saltbytes(); }
public static get crypto_pwhash_STRBYTES() { return Sodium.crypto_pwhash_strbytes(); }
public static get crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE() { return Sodium.crypto_pwhash_scryptsalsa208sha256_memlimit_interactive(); }
public static get crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE() { return Sodium.crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive(); }
public static get crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE() { return Sodium.crypto_pwhash_scryptsalsa208sha256_opslimit_interactive(); }
public static get crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE() { return Sodium.crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive(); }
public static get crypto_pwhash_scryptsalsa208sha256_SALTBYTES() { return Sodium.crypto_pwhash_scryptsalsa208sha256_saltbytes(); }
public static get crypto_pwhash_scryptsalsa208sha256_STRBYTES() { return Sodium.crypto_pwhash_scryptsalsa208sha256_strbytes(); }
public static get crypto_scalarmult_BYTES() { return Sodium.crypto_scalarmult_bytes(); }
public static get crypto_scalarmult_SCALARBYTES() { return Sodium.crypto_scalarmult_scalarbytes(); }
public static get crypto_secretbox_KEYBYTES() { return Sodium.crypto_secretbox_keybytes(); }
public static get crypto_secretbox_MACBYTES() { return Sodium.crypto_secretbox_macbytes(); }
public static get crypto_secretbox_NONCEBYTES() { return Sodium.crypto_secretbox_noncebytes(); }
public static get crypto_shorthash_BYTES() { return Sodium.crypto_shorthash_bytes(); }
public static get crypto_shorthash_KEYBYTES() { return Sodium.crypto_shorthash_keybytes(); }
public static get crypto_sign_BYTES() { return Sodium.crypto_sign_bytes(); }
public static get crypto_sign_PUBLICKEYBYTES() { return Sodium.crypto_sign_publickeybytes(); }
public static get crypto_sign_SECRETKEYBYTES() { return Sodium.crypto_sign_secretkeybytes(); }
public static get crypto_sign_SEEDBYTES() { return Sodium.crypto_sign_seedbytes(); }
public static get crypto_stream_chacha20_KEYBYTES() { return Sodium.crypto_stream_chacha20_keybytes(); }
public static get crypto_stream_chacha20_NONCEBYTES() { return Sodium.crypto_stream_chacha20_noncebytes(); }
public static DEMO_MSG(): string {
let 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