Skip to content

Commit 5a75d78

Browse files
committed
Node SDK make methods static
Some methods in crypto.ts can be static. Change-Id: I65c2331296dbc6d2cd2ba2e26907a29d25df1154 Signed-off-by: grapebaba <[email protected]>
1 parent e7138ae commit 5a75d78

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

sdk/node/src/crypto.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ export class Crypto {
115115
* @params hashAlgorithm The hash algorithm ('SHA2' or 'SHA3')
116116
*/
117117
setHashAlgorithm(hashAlgorithm:string):void {
118-
this.checkHashFunction(hashAlgorithm);
118+
Crypto.checkHashFunction(hashAlgorithm);
119119

120120
this.hashAlgorithm = hashAlgorithm;
121121
this.initialize();
122122
}
123123

124-
generateNonce() {
124+
static generateNonce() {
125125
return crypto.randomBytes(NonceSize);
126126
}
127127

@@ -305,11 +305,11 @@ export class Crypto {
305305
return decryptedBytes;
306306
}
307307

308-
aesKeyGen() {
308+
static aesKeyGen() {
309309
return crypto.randomBytes(AESKeyLength);
310310
}
311311

312-
aesCFBDecryt(key, encryptedBytes) {
312+
static aesCFBDecrypt(key, encryptedBytes) {
313313

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

337337
var decryptedBytes, unpaddedBytes;
338338

339-
decryptedBytes = this.CBCDecrypt(key, bytes);
340-
unpaddedBytes = this.PKCS7UnPadding(decryptedBytes);
339+
decryptedBytes = Crypto.CBCDecrypt(key, bytes);
340+
unpaddedBytes = Crypto.PKCS7UnPadding(decryptedBytes);
341341

342342
return unpaddedBytes;
343343
};
344344

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

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

366366
return bitsToBytes(key);
367367

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

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

@@ -405,7 +405,7 @@ export class Crypto {
405405

406406
private initialize() {
407407
this.checkSecurityLevel(this.securityLevel);
408-
this.checkHashFunction(this.hashAlgorithm);
408+
Crypto.checkHashFunction(this.hashAlgorithm);
409409

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

460460
// Hash = Hash || sjcl.hash.sha256;
@@ -496,7 +496,7 @@ export class Crypto {
496496
return sjcl.bitArray.clamp(ret, keyBitLength);
497497
}
498498

499-
private CBCDecrypt(key, bytes) {
499+
private static CBCDecrypt(key, bytes) {
500500
debug('key length: ', key.length);
501501
debug('bytes length: ', bytes.length);
502502
var iv = bytes.slice(0, BlockSize);
@@ -524,7 +524,6 @@ export class Crypto {
524524
start += BlockSize;
525525
end += BlockSize;
526526
}
527-
;
528527

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

540539
};
541540

542-
private PKCS7UnPadding(bytes) {
541+
private static PKCS7UnPadding(bytes) {
543542

544543
//last byte is the number of padded bytes
545544
var padding = bytes.readUInt8(bytes.length - 1);

sdk/node/src/hfc.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ export class Member {
10101010
if (err) return cb(err);
10111011
self.enrollment = enrollment;
10121012
// Generate queryStateKey
1013-
self.enrollment.queryStateKey = self.chain.cryptoPrimitives.generateNonce();
1013+
self.enrollment.queryStateKey = crypto.Crypto.generateNonce()
10141014

10151015
// Save state
10161016
self.saveState(function (err) {
@@ -1208,7 +1208,7 @@ export class TransactionContext extends events.EventEmitter {
12081208
this.chain = member.getChain();
12091209
this.memberServices = this.chain.getMemberServices();
12101210
this.tcert = tcert;
1211-
this.nonce = this.chain.cryptoPrimitives.generateNonce();
1211+
this.nonce = crypto.Crypto.generateNonce();
12121212
this.complete = false;
12131213
this.timeoutId = null;
12141214
}
@@ -1526,7 +1526,7 @@ export class TransactionContext extends events.EventEmitter {
15261526
var stateKey;
15271527
if (transaction.pb.getType() == _fabricProto.Transaction.Type.CHAINCODE_DEPLOY) {
15281528
// The request is for a deploy
1529-
stateKey = new Buffer(self.chain.cryptoPrimitives.aesKeyGen());
1529+
stateKey = new Buffer(crypto.Crypto.aesKeyGen());
15301530
} else if (transaction.pb.getType() == _fabricProto.Transaction.Type.CHAINCODE_INVOKE ) {
15311531
// The request is for an execute
15321532
// Empty state key
@@ -1603,7 +1603,7 @@ export class TransactionContext extends events.EventEmitter {
16031603
);
16041604

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

16091609
/**

0 commit comments

Comments
 (0)