Commit 3153d895 authored by David Burke's avatar David Burke

pwhash test passes

parent 9b29e1f0
import * as observable from 'tns-core-modules/data/observable';
import * as pages from 'tns-core-modules/ui/page';
import {HelloWorldModel} from './main-view-model';
import { Libsodium } from 'nativescript-libsodium';
const message = Libsodium.DEMO_MSG();
// Event handler for Page 'loaded' event attached in main-page.xml
export function pageLoaded(args: observable.EventData) {
// Get the event sender
let page = <pages.Page>args.object;
page.bindingContext = new HelloWorldModel();
let context = {
message: message,
};
page.bindingContext = context;
}
\ No newline at end of file
import { Observable } from 'tns-core-modules/data/observable';
import { Libsodium } from 'nativescript-libsodium';
export class HelloWorldModel extends Observable {
public message: string;
private libsodium: Libsodium;
constructor() {
super();
this.message = Libsodium.DEMO_MSG();
}
}
\ No newline at end of file
......@@ -7,5 +7,10 @@ describe("libsodium nativescript wrapper", function () {
expect(keypair.privateKey).toBeDefined();
expect(keypair.publicKey).toBeDefined();
});
it("can run crypto_pwhash", function () {
var salt = new Uint8Array([88, 240, 185, 66, 195, 101, 160, 138, 137, 78, 1, 2, 3, 4, 5, 6]);
var hash = nativescript_libsodium_1.Libsodium.crypto_pwhash(nativescript_libsodium_1.Libsodium.crypto_box_SEEDBYTES, "hunter2", salt, nativescript_libsodium_1.Libsodium.crypto_pwhash_OPSLIMIT_INTERACTIVE, nativescript_libsodium_1.Libsodium.crypto_pwhash_MEMLIMIT_INTERACTIVE, nativescript_libsodium_1.Libsodium.crypto_pwhash_ALG_DEFAULT);
expect(hash[0]).toEqual(49);
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJ0ZXN0cy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLGlFQUFtRDtBQUVuRCxRQUFRLENBQUMsZ0NBQWdDLEVBQUU7SUFDdkMsRUFBRSxDQUFDLHdCQUF3QixFQUFFO1FBQ3pCLElBQUksT0FBTy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SBhIGtleXBhaXJcIiwgZnVuY3Rpb24oKSB7XG4gICAgICAgIGxldCBrZXlwYWlyID0gTGlic29kaXVtLmNyeXB0b19ib3hfa2V5cGFpcigpO1xuICAgICAgICBleHBlY3Qoa2V5cGFpci5wcml2YXRlS2V5KS50b0JlRGVmaW5lZCgpO1xuICAgICAgICBleHBlY3Qoa2V5cGFpci5wdWJsaWNLZXkpLnRvQmVEZWZpbmVkKCk7XG4gICAgfSk7XG5cbiAgICBpdChcImNhbiBydW4gY3J5cHRvX3B3aGFzaFwiLCBmdW5jdGlvbigpIHtcbiAgICAgICAgbGV0IHNhbHQgPSBuZXcgVWludDhBcnJheShbIDg4LCAyNDAsIDE4NSwgNjYsIDE5NSwgMTAxLCAxNjAsIDEzOCwgMTM3LCA3OCwgMSwgMiwgMywgNCwgNSwgNl0pO1xuICAgICAgICBsZXQgaGFzaCA9IExpYnNvZGl1bS5jcnlwdG9fcHdoYXNoKFxuICAgICAgICAgICAgTGlic29kaXVtLmNyeXB0b19ib3hfU0VFREJZVEVTLFxuICAgICAgICAgICAgXCJodW50ZXIyXCIsXG4gICAgICAgICAgICBzYWx0LFxuICAgICAgICAgICAgTGlic29kaXVtLmNyeXB0b19wd2hhc2hfT1BTTElNSVRfSU5URVJBQ1RJVkUsXG4gICAgICAgICAgICBMaWJzb2RpdW0uY3J5cHRvX3B3aGFzaF9NRU1MSU1JVF9JTlRFUkFDVElWRSxcbiAgICAgICAgICAgIExpYnNvZGl1bS5jcnlwdG9fcHdoYXNoX0FMR19ERUZBVUxULFxuICAgICAgICApO1xuICAgICAgICBleHBlY3QoaGFzaFswXSkudG9FcXVhbCg0OSk7XG4gICAgfSk7XG59KTtcbiJdfQ==
\ No newline at end of file
......@@ -7,4 +7,16 @@ describe("libsodium nativescript wrapper", function() {
expect(keypair.publicKey).toBeDefined();
});
it("can run crypto_pwhash", function() {
let salt = new Uint8Array([ 88, 240, 185, 66, 195, 101, 160, 138, 137, 78, 1, 2, 3, 4, 5, 6]);
let hash = Libsodium.crypto_pwhash(
Libsodium.crypto_box_SEEDBYTES,
"hunter2",
salt,
Libsodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
Libsodium.crypto_pwhash_MEMLIMIT_INTERACTIVE,
Libsodium.crypto_pwhash_ALG_DEFAULT,
);
expect(hash[0]).toEqual(49);
});
});
export declare type JavaBytes = native.Array<number>;
export interface IKeyPair {
keyType?: string;
privateKey: Uint8Array;
......@@ -6,11 +7,15 @@ export interface IKeyPair {
export declare class Common {
static BASE64_FLAGS: number;
private static javaByteArrayToUint8Array(javaBytes);
static Uint8ArrayToJavaByteArray(inBytes: Uint8Array): native.Array<number>;
static Uint8ArrayToJavaByteArray(inBytes: Uint8Array): JavaBytes;
static to_base64(aBytes: Uint8Array): string;
static from_base64(sBase64: string): Uint8Array;
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): native.Array<number>;
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;
static readonly crypto_box_SEEDBYTES: number;
static DEMO_MSG(): string;
}
......@@ -2,13 +2,18 @@ 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";
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;
NaCl.sodium();
let Base64 = android.util.Base64;
export type JavaBytes = native.Array<number>;
export interface IKeyPair {
keyType?: string;
privateKey: Uint8Array;
......@@ -18,7 +23,7 @@ export interface IKeyPair {
export class Common {
static BASE64_FLAGS = Base64.NO_WRAP | Base64.NO_CLOSE;
private static javaByteArrayToUint8Array(javaBytes: native.Array<number>) {
private static javaByteArrayToUint8Array(javaBytes: JavaBytes) {
let out = new Uint8Array(javaBytes.length);
for (let i = 0; i < javaBytes.length; i++) {
out[i] = javaBytes[i];
......@@ -26,7 +31,7 @@ export class Common {
return out;
}
public static Uint8ArrayToJavaByteArray(inBytes: Uint8Array): native.Array<number> {
public static Uint8ArrayToJavaByteArray(inBytes: Uint8Array): JavaBytes {
let simpleArr = Array.from(inBytes);
return simpleArr;
}
......@@ -59,20 +64,19 @@ export class Common {
};
}
public static crypto_pwhash(keyLength: number, password: String | Uint8Array, salt: Uint8Array, opsLimit: number, memlimit: number, algorithm: number) {
public static crypto_pwhash(keyLength: number, password: string | Uint8Array, salt: Uint8Array, opsLimit: number, memlimit: number, algorithm: number) {
let passwd: Uint8Array;
if (password instanceof String) {
if (typeof(password) === "string") {
passwd = new TextEncoder('utf-8').encode(password);
} else {
passwd = password;
}
let sodium = NaCl.sodium();
let key: native.Array<number> = new byte[keyLength];
let passwordBytes: native.Array<number> = this.Uint8ArrayToJavaByteArray(passwd);
let key: JavaBytes = Array.create('byte', keyLength);
let passwordBytes: JavaBytes = this.Uint8ArrayToJavaByteArray(passwd);
Sodium.crypto_pwhash(
key,
key.length,
keyLength,
passwordBytes,
passwordBytes.length,
this.Uint8ArrayToJavaByteArray(salt),
......@@ -81,9 +85,15 @@ export class Common {
algorithm
);
return key;
let keyBytes = this.javaByteArrayToUint8Array(key);
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(); }
public static get crypto_box_SEEDBYTES() { return Sodium.crypto_box_seedbytes(); }
public static DEMO_MSG(): string {
let msg = '';
......@@ -94,6 +104,22 @@ export class Common {
let privateKey64 = this.to_base64(keypair.privateKey);
msg += `Private key in base64: ${privateKey64}\n`;
let opslimit = this.crypto_pwhash_OPSLIMIT_INTERACTIVE;
msg += `The opslimit constant is ${opslimit}\n`;
let passwd = "hunter2";
let salt = new Uint8Array([ 88, 240, 185, 66, 195, 101, 160, 138, 137, 78, 1, 2, 3, 4, 5, 6]);
let hash = this.crypto_pwhash(
this.crypto_box_SEEDBYTES,
passwd,
salt,
this.crypto_pwhash_OPSLIMIT_INTERACTIVE,
this.crypto_pwhash_MEMLIMIT_INTERACTIVE,
this.crypto_pwhash_ALG_DEFAULT,
);
msg += `The hash is ${hash}\n`;
return msg;
}
}
......@@ -50,6 +50,8 @@
"tslint": "^5.0.0",
"typescript": "~2.3.1"
},
"dependencies": {},
"dependencies": {
"text-encoding": "~0.6.3"
},
"bootstrapper": "nativescript-plugin-seed"
}
/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" />
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" />
declare const TextEncoder: any;
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