Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add browser side jsonformatter

  • Loading branch information...
commit 370131cdba4812a2871041e58ee57882d2b627c6 1 parent c00c85f
Jack Carrick Xiang Cheng authored
Showing with 42 additions and 0 deletions.
  1. +42 −0 client/jsonformatter.js
View
42 client/jsonformatter.js
@@ -0,0 +1,42 @@
+//create custom json serialization format
+var JsonFormatter = {
+ stringify: function (cipherParams) {
+ // create json object with ciphertext
+ var jsonObj = {
+ ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64)
+ };
+
+ // optionally add iv and salt
+ if (cipherParams.iv) {
+ jsonObj.iv = cipherParams.iv.toString();
+ }
+
+ if (cipherParams.salt) {
+ jsonObj.s = cipherParams.salt.toString();
+ }
+
+ // stringify json object
+ return JSON.stringify(jsonObj)
+ },
+
+ parse: function (jsonStr) {
+ // parse json string
+ var jsonObj = JSON.parse(jsonStr);
+
+ // extract ciphertext from json object, and create cipher params object
+ var cipherParams = CryptoJS.lib.CipherParams.create({
+ ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct)
+ });
+
+ // optionally extract iv and salt
+ if (jsonObj.iv) {
+ cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv);
+ }
+
+ if (jsonObj.s) {
+ cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s);
+ }
+
+ return cipherParams;
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.