diff --git a/src/js/crypto/nacl-crypto.js b/src/js/crypto/nacl-crypto.js new file mode 100644 index 0000000..661f967 --- /dev/null +++ b/src/js/crypto/nacl-crypto.js @@ -0,0 +1,29 @@ +/** + * A Wrapper for NaCl's asymmetric/symmetric crypto + */ +app.crypto.NaclCrypto = function() { + 'use strict'; + + /** + * Encrypt a String with the provided keysize (e.g. 128, 256) + * @param plaintext [String] The input string in UTF8 + * @param key [String] The base64 encoded key + * @param iv [String] The base64 encoded IV + * @return [String] The base64 encoded ciphertext + */ + this.encrypt = function(plaintext, key, iv) { + + }; + + /** + * Decrypt a String with the provided keysize (e.g. 128, 256) + * @param ciphertext [String] The base64 encoded ciphertext + * @param key [String] The base64 encoded key + * @param iv [String] The base64 encoded IV + * @return [String] The decrypted plaintext in UTF8 + */ + this.decrypt = function(ciphertext, key, iv) { + + }; + +}; \ No newline at end of file diff --git a/src/test/unit/index.html b/src/test/unit/index.html index 1769833..eba2275 100644 --- a/src/test/unit/index.html +++ b/src/test/unit/index.html @@ -40,6 +40,8 @@ + + @@ -59,6 +61,7 @@ + @@ -71,6 +74,7 @@ + diff --git a/src/test/unit/nacl-crypto-test.js b/src/test/unit/nacl-crypto-test.js new file mode 100644 index 0000000..01c46af --- /dev/null +++ b/src/test/unit/nacl-crypto-test.js @@ -0,0 +1,42 @@ +module("NaCl Crypto"); + +var nacl_test = { + keySize: 128 +}; + +test("Init", 1, function() { + // init dependencies + nacl_test.util = new app.crypto.Util(window, uuid); + ok(nacl_test.util, 'Util'); + // generate test data + nacl_test.test_message = new TestData().generateBigString(1000); +}); + +test("Generate Keypiar", 1, function() { + // generate keypair from seed + var keys = nacl.crypto_box_keypair(); + ok(keys.boxSk && keys.boxPk, "Keysize: " + keys.boxPk.length); + nacl_test.keys = keys; +}); + +test("En/Decrypt", 2, function() { + var naclCrypto = new app.crypto.NaclCrypto(); + + var plaintext = nacl_test.test_message; + // var key = nacl_test.util.random(nacl_test.keySize); + // var iv = nacl_test.util.random(104); + + // convert utf8 string to Uint8Array + var pt = nacl.encode_utf8(plaintext); + // generate nonce + var nonce = nacl.crypto_secretbox_random_nonce(); + // encrypt + var ct = nacl.crypto_secretbox(pt, nonce, nacl_test.keys.boxSk); + ok(ct, 'Ciphertext length: ' + ct.length); + + // decrypt + var decryptedBuf = nacl.crypto_secretbox_open(ct, nonce, nacl_test.keys.boxSk); + var decrypted = nacl.decode_utf8(decryptedBuf); + equal(decrypted, plaintext, 'Decryption correct: ' + decrypted); + +}); \ No newline at end of file