Skip to content

Commit

Permalink
fixed the serialization issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hortinstein committed Jan 25, 2023
1 parent 135e094 commit e5cd63f
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TYPESCRIPT_TEST_FILE
TESTFILE_*
11 changes: 11 additions & 0 deletions src/enkodo/serialize.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ proc unwrap*(b64SerEncObj:string): EncObj =
var encObj = desEncObj(serEncObj)
return encObj

proc wrapKey*(key: Key): string =
let serKey = toFlatty(key)
var b64Key = b64Str(serKey)
return b64Key

proc unwrapKey*(wrappedKey: string): Key =
let serKey = unb64str(wrappedKey)
return serKey.fromFlatty(Key)

when defined(js):
module.exports.returnEncObj = returnEncObj
module.exports.serEncObj = serEncObj
Expand All @@ -48,3 +57,5 @@ when defined(js):
module.exports.unb64Str = unb64Str
module.exports.wrap = wrap
module.exports.unwrap = unwrap
module.exports.wrapKey = wrapKey
module.exports.unwrapKey = unwrapKey
27 changes: 18 additions & 9 deletions tests/test_nim.nim
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# This is just an example to get you started. You may wish to put all of your
# tests into a single file, or separate them into multiple `test1`, `test2`
# etc. files (better names are recommended, just make sure the name starts with
# the letter 't').
#
# To run these tests, simply execute `nimble test`.

import unittest

import os
import sysrandom
import std/base64

import flatty

import enkodo
import enkodo/types
import enkodo/serialize

proc writeStringToFile*(fileName: string, contents: string) =
let f = open(filename, fmWrite)
f.write(contents)
defer: f.close()

let (a_secretKey, a_publicKey) = generateKeyPair()
let (b_secretKey, b_publicKey) = generateKeyPair()
let plaintext = cast[seq[byte]]("hello this is a test string")
Expand Down Expand Up @@ -52,8 +53,16 @@ test "testing wrap,unwrap":
let unwrapped = unwrap(wrapped)
let ptext = dec(b_secretKey,unwrapped)
doAssert(plaintext == ptext)

test "outputting test file for JS to read and decrypt":
let encObj = enc(a_secretKey,b_publicKey,plaintext)

let b64Str = wrap(encObj)

writeStringToFile("TESTFILE_TYPESCRIPT_ENC_BIN", b64Str)

# test "testing enc on a blank message":
writeStringToFile("TESTFILE_TYPESCRIPT_PRIVKEY", wrapKey(b_secretKey))
# test "testing enc on a blank message":
# let encObj = encObj(a_secretKey,b_publicKey,'')

# test "testing dec with wrong keys":
Expand Down
12 changes: 10 additions & 2 deletions typescript/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,18 @@ export function generateKeyPair(): [Uint8Array, Uint8Array] {
return [privateKey, publicKey];
}

export function wrap(obj: any): string {
export function wrap(obj: any): any {
return serialize.wrap(obj);
}

export function unwrap(wrappedObj: string): any {
export function unwrap(wrappedObj: any): any {
return serialize.unwrap(wrappedObj);
}

export function wrapKey(obj: any): any {
return serialize.wrapKey(obj);
}

export function unwrapKey(wrappedObj: any): any {
return serialize.unwrapKey(wrappedObj);
}
25 changes: 24 additions & 1 deletion typescript/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,27 @@ import {
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { createRequire } from "https://deno.land/[email protected]/node/module.ts";

import { enc, dec, wrap, unwrap, generateKeyPair } from "./main.ts";
import {
enc,
dec,
wrap,
unwrap,
wrapKey,
unwrapKey,
generateKeyPair
} from "./main.ts";

const hello_world = new TextEncoder().encode("Hello World");
const [priv, pub] = generateKeyPair();
const [priv2, pub2] = generateKeyPair();

const base64ToUint8 = (str: string): Uint8Array =>
Uint8Array.from(atob(str), (c) => c.charCodeAt(0));

// uses the release config for the tests
const TEST_STRING = new TextEncoder().encode("hello this is a test string");
const TEST_WRAPPED = await Deno.readFile("./TESTFILE_TYPESCRIPT_ENC_BIN")
const TEST_KEY = await Deno.readFile("./TESTFILE_TYPESCRIPT_PRIVKEY")

// this function tests whether the encryption and decryption functions work
export function encrypt_decrypt() {
Expand All @@ -24,7 +39,15 @@ Deno.test(encrypt_decrypt)
Deno.test(function wrap_unwrap() {
const enc_test = enc(priv, pub2, hello_world);
const wrapped = wrap(enc_test);
console.log(wrapped)
const unwrapped = unwrap(wrapped);
const plain = dec(priv2, unwrapped);
assertEquals(enc_test, unwrapped);
});

Deno.test(function unwrapanddecrypt() {
const unwrapped = unwrap(TEST_WRAPPED);
const unwrappedKey = unwrapKey(TEST_KEY)
const plain = dec(unwrappedKey, unwrapped);
assertEquals(plain, TEST_STRING)
});
8 changes: 8 additions & 0 deletions typescript/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57912,9 +57912,17 @@ function wrap(obj) {
function unwrap(wrappedObj) {
return serialize.unwrap(wrappedObj);
}
function wrapKey(obj) {
return serialize.wrapKey(obj);
}
function unwrapKey(wrappedObj) {
return serialize.unwrapKey(wrappedObj);
}
export { enc as enc };
export { dec as dec };
export { generateKeyPair as generateKeyPair };
export { wrap as wrap };
export { unwrap as unwrap };
export { wrapKey as wrapKey };
export { unwrapKey as unwrapKey };

Loading

0 comments on commit e5cd63f

Please sign in to comment.