Commit 2f579591 authored by David Burke's avatar David Burke

Some functions work

parent 25ca6aaa
var Libsodium = require("nativescript-libsodium").Libsodium;
var libsodium = new Libsodium();
describe("greet function", function() {
it("exists", function() {
describe("greet function", function () {
it("exists", function () {
expect(libsodium.greet).toBeDefined();
});
it("returns a string", function() {
it("returns a string", function () {
expect(libsodium.greet()).toEqual("Hello, NS");
});
});
\ No newline at end of file
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJ0ZXN0cy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxJQUFJLFNBQVMsR0FBRyxPQUFPLENBQUMsd0JBQXdCLENBQUMsQ0FBQyxTQUFTLENBQUM7QUFDNUQsSUFBSSxTQUFTLEdBQUcsSUFBSSxTQUFTLEVBQUUsQ0FBQztBQUVoQyxRQUFRLENBQUMsZ0JBQWdCLEVBQUU7SUFDdkIsRUFBRSxDQUFDLFFBQVEsRUFBRTtRQUNULE1BQU0sQ0FBQyxTQUFTLENBQUMsS0FBSyxDQUFDLENBQUMsV0FBVyxFQUFFLENBQUM7SUFDMUMsQ0FBQyxDQUFDLENBQUM7SUFFSCxFQUFFLENBQUMsa0JBQWtCLEVBQUU7UUFDbkIsTUFBTSxDQUFDLFNBQVMsQ0FBQyxLQUFLLEVBQUUsQ0FBQyxDQUFDLE9BQU8sQ0FBQyxXQUFXLENBQUMsQ0FBQztJQUNuRCxDQUFDLENBQUMsQ0FBQztBQUNQLENBQUMsQ0FBQyxDQUFDIiwic291cmNlc0NvbnRlbnQiOlsibGV0IExpYnNvZGl1bSA9IHJlcXVpcmUoXCJuYXRpdmVzY3JpcHQtbGlic29kaXVtXCIpLkxpYnNvZGl1bTtcbmxldCBsaWJzb2RpdW0gPSBuZXcgTGlic29kaXVtKCk7XG5cbmRlc2NyaWJlKFwiZ3JlZXQgZnVuY3Rpb25cIiwgZnVuY3Rpb24oKSB7XG4gICAgaXQoXCJleGlzdHNcIiwgZnVuY3Rpb24oKSB7XG4gICAgICAgIGV4cGVjdChsaWJzb2RpdW0uZ3JlZXQpLnRvQmVEZWZpbmVkKCk7XG4gICAgfSk7XG5cbiAgICBpdChcInJldHVybnMgYSBzdHJpbmdcIiwgZnVuY3Rpb24oKSB7XG4gICAgICAgIGV4cGVjdChsaWJzb2RpdW0uZ3JlZXQoKSkudG9FcXVhbChcIkhlbGxvLCBOU1wiKTtcbiAgICB9KTtcbn0pO1xuIl19
\ No newline at end of file
let Libsodium = require("nativescript-libsodium").Libsodium;
let libsodium = new Libsodium();
describe("greet function", function() {
it("exists", function() {
expect(libsodium.greet).toBeDefined();
});
it("returns a string", function() {
expect(libsodium.greet()).toEqual("Hello, NS");
});
});
{
"nativescript": {
"id": "org.nativescript.demo",
"tns-ios": {
"version": "3.0.1"
},
"tns-android": {
"version": "3.1.1"
}
"nativescript": {
"id": "org.nativescript.demo",
"tns-ios": {
"version": "3.0.1"
},
"dependencies": {
"nativescript-theme-core": "^1.0.4",
"nativescript-unit-test-runner": "^0.3.4",
"nativescript-libsodium": "../src",
"tns-core-modules": "^3.1.0"
},
"devDependencies": {
"babel-traverse": "6.12.0",
"babel-types": "6.11.1",
"babylon": "6.8.4",
"filewalker": "0.1.2",
"jasmine-core": "^2.5.2",
"karma": "^1.3.0",
"karma-jasmine": "^1.0.2",
"karma-nativescript-launcher": "^0.4.0",
"lazy": "1.0.11",
"nativescript-dev-typescript": "libs",
"typescript": "~2.3.1"
"tns-android": {
"version": "3.1.1"
}
}
\ No newline at end of file
},
"dependencies": {
"nativescript-libsodium": "../src",
"nativescript-theme-core": "^1.0.4",
"nativescript-unit-test-runner": "^0.3.4",
"tns-core-modules": "^3.1.0"
},
"devDependencies": {
"@types/jasmine": "^2.5.53",
"babel-traverse": "6.12.0",
"babel-types": "6.11.1",
"babylon": "6.8.4",
"filewalker": "0.1.2",
"jasmine-core": "^2.5.2",
"karma": "^1.3.0",
"karma-jasmine": "^1.0.2",
"karma-nativescript-launcher": "^0.4.0",
"lazy": "1.0.11",
"nativescript-dev-typescript": "libs",
"typescript": "~2.3.1"
}
}
This diff is collapsed.
......@@ -2,8 +2,20 @@ import { Observable } from 'tns-core-modules/data/observable';
export declare class Common extends Observable {
message: string;
constructor();
greet(): string;
}
export declare class Utils {
export interface IKeyPair {
keyType?: string;
privateKey: Uint8Array;
publicKey: Uint8Array;
}
export declare class SodiumWrapper {
static BASE64_FLAGS: number;
private static javaByteArrayToUint8Array(javaBytes);
static Uint8ArrayToJavaByteArray(inBytes: Uint8Array): native.Array<number>;
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_pwhashcrypto_pwhash(keyLength: number, password: String | Uint8Array, salt: Uint8Array, opsLimit: number, memlimit: number, algorithm: number): native.Array<number>;
static SUCCESS_MSG(): string;
}
......@@ -2,26 +2,106 @@ 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';
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;
let Base64 = android.util.Base64;
export class Common extends Observable {
public message: string;
constructor() {
super();
this.message = Utils.SUCCESS_MSG();
this.message = SodiumWrapper.SUCCESS_MSG();
}
}
public greet() {
return "Hello, NS";
}
export interface IKeyPair {
keyType?: string;
privateKey: Uint8Array;
publicKey: Uint8Array;
}
export class Utils {
public static SUCCESS_MSG(): string {
let msg = `Your plugin is working on ${app.android ? 'Android' : 'iOS'}.`;
export class SodiumWrapper {
static BASE64_FLAGS = Base64.NO_WRAP | Base64.NO_CLOSE;
private static javaByteArrayToUint8Array(javaBytes: native.Array<number>) {
let out = new Uint8Array(javaBytes.length);
for (let i = 0; i < javaBytes.length; i++) {
out[i] = javaBytes[i];
}
return out;
}
public static Uint8ArrayToJavaByteArray(inBytes: Uint8Array): native.Array<number> {
let simpleArr = Array.from(inBytes);
return simpleArr;
}
public static to_base64(aBytes: Uint8Array): string {
let javaArr = this.Uint8ArrayToJavaByteArray(aBytes);
let out = Base64.encodeToString(javaArr, this.BASE64_FLAGS);
return out;
}
setTimeout(() => {
dialogs.alert(`${msg} For real. It's really working :)`).then(() => console.log(`Dialog closed.`));
}, 2000);
public static from_base64(sBase64: string): Uint8Array {
let out = Base64.decode(sBase64, this.BASE64_FLAGS);
return this.javaByteArrayToUint8Array(out);
}
public static randombytes_buf(length: number): Uint8Array {
let rand = new Random().randomBytes(length);
return this.javaByteArrayToUint8Array(rand);
}
public static crypto_box_keypair(): IKeyPair {
let rand = new Random().randomBytes(SodiumConstants.SECRETKEY_BYTES);
let keys = new org.libsodium.jni.keys.KeyPair(rand);
let publicKey = keys.getPublicKey();
let privateKey = keys.getPrivateKey();
return {
privateKey: this.javaByteArrayToUint8Array(privateKey.toBytes()),
publicKey: this.javaByteArrayToUint8Array(publicKey.toBytes()),
keyType: "curve25519"
};
}
public static crypto_pwhashcrypto_pwhash(keyLength: number, password: String | Uint8Array, salt: Uint8Array, opsLimit: number, memlimit: number, algorithm: number) {
let passwd: Uint8Array;
if (password instanceof 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);
Sodium.crypto_pwhash(
key,
key.length,
passwordBytes,
passwordBytes.length,
this.Uint8ArrayToJavaByteArray(salt),
opsLimit,
memlimit,
algorithm
);
return key;
}
public static SUCCESS_MSG(): string {
// let rand = this.randombytes_buf(org.libsodium.jni.SodiumConstants.SECRETKEY_BYTES);
let x = this.crypto_box_keypair();
let key = x.privateKey;
let bb = new Uint8Array([1, 1, 1]);
console.log(typeof(key));
console.log(typeof(bb));
let y = this.to_base64(key);
let msg = `Your plugin is working on ${y}`;
return msg;
}
......
{
"name": "nativescript-libsodium",
"version": "1.0.0",
"description": "Your awesome NativeScript plugin.",
"main": "libsodium",
"typings": "index.d.ts",
"nativescript": {
"platforms": {
"android": "3.1.1"
}
},
"scripts": {
"tsc": "tsc -skipLibCheck",
"postclone": "npm i && node scripts/postclone.js && cd ../demo && npm i && cd ../src && npm run plugin.link",
"test.android": "npm i && npm run tsc && npm run tslint && cd ../demo && tns build android && tns test android --justlaunch",
"test.ios": "npm i && npm run tsc && npm run tslint && cd ../demo && tns build ios && tns test ios --justlaunch",
"tslint": "cd .. && tslint \"**/*.ts\" --config tslint.json --exclude \"**/node_modules/**\"",
"plugin.link": "npm link && cd ../demo && npm link nativescript-libsodium && cd ../src",
"plugin.tscwatch": "npm run tsc -- -w",
"demo.ios": "npm i && npm run tsc && cd ../demo && tns run ios --syncAllFiles",
"demo.android": "npm i && npm run tsc && cd ../demo && tns run android --syncAllFiles"
},
"repository": {
"type": "git",
"url": "https://gitlab.com/burke-software/nativescript-libsodium.git"
},
"keywords": [
"NativeScript",
"JavaScript",
"Android"
],
"author": {
"name": "Your Name",
"email": "youremail@yourdomain.com"
},
"bugs": {
"url": "https://gitlab.com/burke-software/nativescript-libsodium/issues"
},
"license": "Apache-2.0",
"homepage": "https://gitlab.com/burke-software/nativescript-libsodium",
"readmeFilename": "README.md",
"peerDependencies": {
"tns-core-modules": "^3.1.0"
},
"devDependencies": {
"tns-core-modules": "^3.1.0",
"tns-platform-declarations": "^3.1.0",
"typescript": "~2.3.1",
"prompt": "^1.0.0",
"rimraf": "^2.5.0",
"tslint": "^5.0.0"
},
"dependencies": {},
"bootstrapper": "nativescript-plugin-seed"
}
\ No newline at end of file
"name": "nativescript-libsodium",
"version": "1.0.0",
"description": "Your awesome NativeScript plugin.",
"main": "libsodium",
"typings": "index.d.ts",
"nativescript": {
"platforms": {
"android": "3.1.1"
}
},
"scripts": {
"tsc": "tsc -skipLibCheck",
"postclone": "npm i && node scripts/postclone.js && cd ../demo && npm i && cd ../src && npm run plugin.link",
"test.android": "npm i && npm run tsc && npm run tslint && cd ../demo && tns build android && tns test android --justlaunch",
"test.ios": "npm i && npm run tsc && npm run tslint && cd ../demo && tns build ios && tns test ios --justlaunch",
"tslint": "cd .. && tslint \"**/*.ts\" --config tslint.json --exclude \"**/node_modules/**\"",
"plugin.link": "npm link && cd ../demo && npm link nativescript-libsodium && cd ../src",
"plugin.tscwatch": "npm run tsc -- -w",
"demo.ios": "npm i && npm run tsc && cd ../demo && tns run ios --syncAllFiles",
"demo.android": "npm i && npm run tsc && cd ../demo && tns run android --syncAllFiles"
},
"repository": {
"type": "git",
"url": "https://gitlab.com/burke-software/nativescript-libsodium.git"
},
"keywords": [
"NativeScript",
"JavaScript",
"Android"
],
"author": {
"name": "Your Name",
"email": "youremail@yourdomain.com"
},
"bugs": {
"url": "https://gitlab.com/burke-software/nativescript-libsodium/issues"
},
"license": "Apache-2.0",
"homepage": "https://gitlab.com/burke-software/nativescript-libsodium",
"readmeFilename": "README.md",
"peerDependencies": {
"tns-core-modules": "^3.1.0"
},
"devDependencies": {
"prompt": "^1.0.0",
"rimraf": "^2.5.0",
"tns-core-modules": "^3.1.0",
"tns-platform-declarations": "^3.1.0",
"tslint": "^5.0.0",
"typescript": "~2.3.1"
},
"dependencies": {},
"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