0.0.4
[henge/kiak.git] / src / strappCrypto.js
diff --git a/src/strappCrypto.js b/src/strappCrypto.js
new file mode 100644 (file)
index 0000000..e34db01
--- /dev/null
@@ -0,0 +1,60 @@
+/**
+ * @file      Management for bootstrapp cryptography 
+ * @desc      Makes keys, encrypts and decrypts messages
+ *            
+ * @author    Jordan Lavatai and Ken Grimes
+ * @version   0.0.1
+ * @license   AGPL-3.0
+ * @copyright Strapp.io
+ */
+
+/* TODO: Replace with filesystem operations */
+import {setItem, getItem} from "localForage"
+
+/** @func Generates a CryptoKey and returns a SHA-256 client key
+ *  @desc Utilizes Web Crypto to create a CryptoKey using RSA specification. 
+ *        Stores both public and private representation of the key and returns 
+ *        the public key 
+ *  @return {String} clientKey
+ */
+function generateKey() { 
+  crypto.subtle.generateKey(
+    { name:'RSA-OAEP',
+      modulusLength: 2048,
+      publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
+      hash: {name: "SHA-256"}
+    },
+    true,
+    ['encrypt', 'decrypt']
+  ).then((cryptoKey) => {
+    /* TODO: Do we need to store the private key as well? */
+    crypto.subtle.exportKey('jwk', cryptoKey)
+      .then((exportedKey) => {
+        setItem('publicKey', exportedKey.publicKey)
+       setItem('privateKey', exportedKey.privateKey)
+        console.log('public key is' + getItem('publicKey'))
+        console.log('private key is' + getItem('privateKey'))
+        return exportedKey.publicKey
+      })
+  })
+}
+
+/** @func Encrypts data with a public key assuming RSA
+ *  @desc https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
+ *  @arg {String} Public Key      
+ *  @return {Object} The encrypted data as a promise
+ */
+function encryptData(publicKey, data) {
+  return crypto.subtle.encrypt({"name": "RSA-OAEP"})
+}
+/** @func Decrypts data with a private key
+ *  @desc https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
+ *  @arg {String} Private key to decrypt data      
+ *  @return {Object} The decrypted data as a promise
+ */
+function decryptData(privateKey, cipherText) {
+  /* TODO: Pass in private key or get it from localForage? */
+  return crypto.subtle.decrypt({"name":"RSA-OAEP"}, privateKey, cipherText)
+}
+