From 545b7f8a87845edfec385dde0f14191875e4f466 Mon Sep 17 00:00:00 2001 From: joelbandi Date: Sat, 30 Jul 2016 05:36:22 -0700 Subject: [PATCH] more js --- src/java/iotcloud/Entry.java | 2 +- src/java/iotcloud/Slot.java | 12 ++++---- src/js/iotjs/orig/slot.js | 58 +++++++++++++++++++++++++++++++----- src/js/iotjs/package.json | 1 + src/js/iotjs/src/main.js | 45 ++++++++++++++-------------- 5 files changed, 80 insertions(+), 38 deletions(-) diff --git a/src/java/iotcloud/Entry.java b/src/java/iotcloud/Entry.java index 70f90ee..5e0f3f8 100644 --- a/src/java/iotcloud/Entry.java +++ b/src/java/iotcloud/Entry.java @@ -29,7 +29,7 @@ abstract class Entry implements Liveness { * byte tells the type of entry. */ - static Entry decode(Slot slot, ByteBuffer bb) { + slottatic Entry decode(Slot slot, ByteBuffer bb) { byte type=bb.get(); switch(type) { case TypeKeyValue: diff --git a/src/java/iotcloud/Slot.java b/src/java/iotcloud/Slot.java index ed577a6..1d84baa 100644 --- a/src/java/iotcloud/Slot.java +++ b/src/java/iotcloud/Slot.java @@ -21,18 +21,18 @@ class Slot implements Liveness { /** Sequence number of the slot. */ private long seqnum;// /** HMAC of previous slot. */ - private byte[] prevhmac; + private byte[] prevhmac;// /** HMAC of this slot. */ - private byte[] hmac; + private byte[] hmac;// /** Machine that sent this slot. */ - private long machineid; + private long machineid;// /** Vector of entries in this slot. */ private Vector entries; /** Pieces of information that are live. */ - private int livecount; + private int livecount;// /** Flag that indicates whether this slot is still live for * recording the machine that sent it. */ - private boolean seqnumlive; + private boolean seqnumlive;// /** Number of bytes of free space. */ private int freespace; @@ -52,7 +52,7 @@ class Slot implements Liveness { } Slot(long _seqnum, long _machineid) { - this(_seqnum, _machineid, new byte[HMAC_SIZE], null); + this(_seqnum, _machineid, new byte[ HMAC_SIZE], null); } byte[] getHMAC() { diff --git a/src/js/iotjs/orig/slot.js b/src/js/iotjs/orig/slot.js index 72352d9..35ca266 100644 --- a/src/js/iotjs/orig/slot.js +++ b/src/js/iotjs/orig/slot.js @@ -1,10 +1,52 @@ -class Slot{ - constructor(seqnum,machineid,prevhmac,hmac){ - this.SLOT_SIZE=2048; - this.RESERVED_SPACE=64; - this.HMAC_SIZE=32; - (typeof seqnum === "number")? this.seqnum = seqnum : throw new Error("seqnum should be a number"); - (typeof machineid === "number")? this.machineid = seqnum : throw new Error("seqnum should be a number"); - +class Slot { + constructor(seqnum, machineid, prevhmac, hmac) { + this.SLOT_SIZE = 2048; + this.RESERVED_SPACE = 64; + this.HMAC_SIZE = 32; + (typeof seqnum === "number") ? this.seqnum = seqnum: throw new Error("seqnum should be a number"); + (typeof machineid === "number") ? this.machineid = seqnum: throw new Error("machine should be a number"); + this.livecount = 1; + this.seqnumlive = true; + (prevhmac && prevhmac instanceof Uint8Array) ? this.prevhmac = prevhmac: this.prevhmac = new Uint8Array(this.HMAC_SIZE)); + (hmac && hmac instanceof Uint8Array) ? this.hmac = hmac: this.hmac = null; + this.entries = []; + this.freespace = this.SLOT_SIZE - getBaseSize(); //??????? + } + getHMAC() { + return this.hmac; + } + getPrevHmac() { + return this.prevhmac; + } + addEntry(entry) { + if (entry && entry instanceof Entry) { + var obj; + this.entries.push(_.extend(obj, entry)); + this.livecount++; + freespace -= entry.getSize(); + } + } + addShallowEntry(entry){ + if(entry && entry instanceof Entry){ + this.entries.push(entry); + this.livecount++; + freespace -= entry.getSize(); + } + } + hasSpace(entry){ + var newfreespace = this.freespace - entry.getSize(); + return newfreespace > this.RESERVED_SPACE; + } + canFit(entry){ + var newfreespace = this.freespace - entry.getSize(); + return newfreespace >= 0; + } + getEntries(){ + return this.entries; + } + static decode(array){ + var cond1 = (array && array instanceof Uint8Array); + if(cond1){ + } } } \ No newline at end of file diff --git a/src/js/iotjs/package.json b/src/js/iotjs/package.json index 2b12bbe..277b0ba 100644 --- a/src/js/iotjs/package.json +++ b/src/js/iotjs/package.json @@ -20,6 +20,7 @@ "gulp-uglify": "1.1.0", "jshint-stylish": "1.0.1", "minimist": "1.1.1", + "node-forge": "^0.6.41", "object-hash": "^1.1.3", "request": "^2.74.0", "underscore": "^1.8.3" diff --git a/src/js/iotjs/src/main.js b/src/js/iotjs/src/main.js index 78e6a07..b4485fd 100644 --- a/src/js/iotjs/src/main.js +++ b/src/js/iotjs/src/main.js @@ -1,27 +1,21 @@ /* jshint devel:true */ "use strict"; -console.log('Welcome to iotjs-www','\n\n'); - +console.log('Welcome to iotjs-www', '\n\n'); // set up the base line.. // Using browserify to set up browser exports var crypto = require('crypto-js'); -var hash = require('object-hash'); var _ = require('underscore'); -var ByteBuffer = require("bytebuffer"); - - +var ByteBuffer = require('bytebuffer'); +var hash = require('object-hash'); +var forge = require('node-forge'); (function() { - var root = this; - - // iot namespace main constructor var iot = function(obj) { if (obj instanceof iot) return obj; if (!(this instanceof iot)) return new iot(obj); this.iot = obj; }; - // export iot to the global exports if (typeof exports !== 'undefined') { if (typeof module !== 'undefined' && module.exports) { @@ -32,36 +26,41 @@ var ByteBuffer = require("bytebuffer"); root.iot = iot; } - iot.baselinetest = function(){ - // baseline test + + iot.baselinetest = function() { + // baseline test console.log('its alive!!!'); console.log(); + //local hash test console.log(hash('hello man')); console.log(typeof hash('hello man')); console.log(); + //Pair test - var p = new Pair(1,2); + var p = new Pair(1, 2); console.log(p.toString()); console.log(); + //iotstring test var i = new IoTString('hello'); console.log(i.length()); console.log(); - } - - - - - - - - - + //local crypto test + var data = [{id: 1}, {id: 2}]; + // Encrypt + var ciphertext = crypto.AES.encrypt(JSON.stringify(data), 'secret key 123'); + // Decrypt + var bytes = crypto.AES.decrypt(ciphertext.toString(), 'secret key 123'); + var decryptedData = JSON.parse(bytes.toString(crypto.enc.Utf8)); + // console.log(decryptedData); + var e = crypto.HmacMD5("Mess", "Secret Passphrase"); + console.log(typeof e.toString()); + } }()) \ No newline at end of file -- 2.34.1