Skip to content

Commit

Permalink
Node SDK make methods static
Browse files Browse the repository at this point in the history
Some methods in crypto.ts can be static.

Change-Id: I65c2331296dbc6d2cd2ba2e26907a29d25df1154
Signed-off-by: grapebaba <[email protected]>
  • Loading branch information
GrapeBaBa committed Sep 20, 2016
1 parent e7138ae commit 5a75d78
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
27 changes: 13 additions & 14 deletions sdk/node/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ export class Crypto {
* @params hashAlgorithm The hash algorithm ('SHA2' or 'SHA3')
*/
setHashAlgorithm(hashAlgorithm:string):void {
this.checkHashFunction(hashAlgorithm);
Crypto.checkHashFunction(hashAlgorithm);

this.hashAlgorithm = hashAlgorithm;
this.initialize();
}

generateNonce() {
static generateNonce() {
return crypto.randomBytes(NonceSize);
}

Expand Down Expand Up @@ -305,11 +305,11 @@ export class Crypto {
return decryptedBytes;
}

aesKeyGen() {
static aesKeyGen() {
return crypto.randomBytes(AESKeyLength);
}

aesCFBDecryt(key, encryptedBytes) {
static aesCFBDecrypt(key, encryptedBytes) {

var iv = crypto.randomBytes(IVLength);
var aes = new aesjs.ModeOfOperation.cfb(key, iv, IVLength);
Expand All @@ -336,13 +336,13 @@ export class Crypto {

var decryptedBytes, unpaddedBytes;

decryptedBytes = this.CBCDecrypt(key, bytes);
unpaddedBytes = this.PKCS7UnPadding(decryptedBytes);
decryptedBytes = Crypto.CBCDecrypt(key, bytes);
unpaddedBytes = Crypto.PKCS7UnPadding(decryptedBytes);

return unpaddedBytes;
};

aes256GCMDecrypt(key:Buffer, ct:Buffer) {
static aes256GCMDecrypt(key:Buffer, ct:Buffer) {
let decipher = crypto.createDecipheriv('aes-256-gcm', key, ct.slice(0, GCMStandardNonceSize));
decipher.setAuthTag(ct.slice(ct.length - GCMTagSize));
let dec = decipher.update(
Expand All @@ -361,7 +361,7 @@ export class Crypto {
if (!info)
info = "";

var key = this.hkdf2(bytesToBits(new Buffer(ikm)), keyBitLength, bytesToBits(salt), info, this.hashFunctionKeyDerivation);
var key = Crypto.hkdf2(bytesToBits(new Buffer(ikm)), keyBitLength, bytesToBits(salt), info, this.hashFunctionKeyDerivation);

return bitsToBytes(key);

Expand Down Expand Up @@ -394,7 +394,7 @@ export class Crypto {
throw new Error("Illegal level: " + this.securityLevel + " - must be either 256 or 384");
}

private checkHashFunction(hashAlgorithm: string) {
private static checkHashFunction(hashAlgorithm: string) {
if (!_isString(hashAlgorithm))
throw new Error("Illegal Hash function family: " + hashAlgorithm + " - must be either SHA2 or SHA3");

Expand All @@ -405,7 +405,7 @@ export class Crypto {

private initialize() {
this.checkSecurityLevel(this.securityLevel);
this.checkHashFunction(this.hashAlgorithm);
Crypto.checkHashFunction(this.hashAlgorithm);

this.suite = this.hashAlgorithm.toLowerCase() + '-' + this.securityLevel;
if (this.securityLevel == CURVE_P_256_Size) {
Expand Down Expand Up @@ -454,7 +454,7 @@ export class Crypto {
* @param {Object} [Hash=sjcl.hash.sha256] The hash function to use.
* @return {bitArray} derived key.
*/
private hkdf2(ikm, keyBitLength, salt, info, Hash) {
private static hkdf2(ikm, keyBitLength, salt, info, Hash) {
var hmac, key, i, hashLen, loops, curOut, ret = [];

// Hash = Hash || sjcl.hash.sha256;
Expand Down Expand Up @@ -496,7 +496,7 @@ export class Crypto {
return sjcl.bitArray.clamp(ret, keyBitLength);
}

private CBCDecrypt(key, bytes) {
private static CBCDecrypt(key, bytes) {
debug('key length: ', key.length);
debug('bytes length: ', bytes.length);
var iv = bytes.slice(0, BlockSize);
Expand Down Expand Up @@ -524,7 +524,6 @@ export class Crypto {
start += BlockSize;
end += BlockSize;
}
;

decryptedBytes = Buffer.concat(decryptedBlocks);
}
Expand All @@ -539,7 +538,7 @@ export class Crypto {

};

private PKCS7UnPadding(bytes) {
private static PKCS7UnPadding(bytes) {

//last byte is the number of padded bytes
var padding = bytes.readUInt8(bytes.length - 1);
Expand Down
8 changes: 4 additions & 4 deletions sdk/node/src/hfc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ export class Member {
if (err) return cb(err);
self.enrollment = enrollment;
// Generate queryStateKey
self.enrollment.queryStateKey = self.chain.cryptoPrimitives.generateNonce();
self.enrollment.queryStateKey = crypto.Crypto.generateNonce()

// Save state
self.saveState(function (err) {
Expand Down Expand Up @@ -1208,7 +1208,7 @@ export class TransactionContext extends events.EventEmitter {
this.chain = member.getChain();
this.memberServices = this.chain.getMemberServices();
this.tcert = tcert;
this.nonce = this.chain.cryptoPrimitives.generateNonce();
this.nonce = crypto.Crypto.generateNonce();
this.complete = false;
this.timeoutId = null;
}
Expand Down Expand Up @@ -1526,7 +1526,7 @@ export class TransactionContext extends events.EventEmitter {
var stateKey;
if (transaction.pb.getType() == _fabricProto.Transaction.Type.CHAINCODE_DEPLOY) {
// The request is for a deploy
stateKey = new Buffer(self.chain.cryptoPrimitives.aesKeyGen());
stateKey = new Buffer(crypto.Crypto.aesKeyGen());
} else if (transaction.pb.getType() == _fabricProto.Transaction.Type.CHAINCODE_INVOKE ) {
// The request is for an execute
// Empty state key
Expand Down Expand Up @@ -1603,7 +1603,7 @@ export class TransactionContext extends events.EventEmitter {
);

debug('Decrypt Result [%s]', ct.toString('hex'));
return this.chain.cryptoPrimitives.aes256GCMDecrypt(key, ct);
return crypto.Crypto.aes256GCMDecrypt(key, ct);
}

/**
Expand Down

0 comments on commit 5a75d78

Please sign in to comment.