Skip to content

Commit 612b4ae

Browse files
committed
aes256 and bugfix
1 parent c2c31b8 commit 612b4ae

6 files changed

Lines changed: 83 additions & 22 deletions

File tree

lib/crypto.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ function bytesToBits(bytes) {
186186
return out;
187187
}
188188

189-
function paddingPass(password){
190-
if(password.length < 16){
189+
function paddingPass(password,keyLen){
190+
if(password.length < keyLen){
191191
var pass = new Buffer(password);
192-
var retByte = new Buffer(16);
193-
var byteToPad = 16 - password.length;
194-
for(var i=0; i<16; i++){
192+
var retByte = new Buffer(keyLen);
193+
var byteToPad = keyLen - password.length;
194+
for(var i=0; i<keyLen; i++){
195195
if(i<pass.length)
196196
retByte[i] = pass[i];
197197
else
@@ -209,10 +209,10 @@ function paddingPass(password){
209209
* @returns string 加密后的十六进制格式
210210
*/
211211
var aesEncrypt = function(secret, plaintext) {
212-
var secretPadded = paddingPass(secret);
212+
var secretPadded = paddingPass(secret,AESKeyLength);
213213
var aesKey = new Buffer(secretPadded, 'utf8');
214214
var iv = aesKey.slice(0, IVLength);
215-
var cipher = crypto.createCipheriv('aes-128-cbc', aesKey, iv);
215+
var cipher = crypto.createCipheriv('aes-256-cbc', aesKey, iv);
216216
var plainBuf = new Buffer(plaintext, 'utf8');
217217
var encryptedBytes = cipher.update(plainBuf);
218218
encryptedBytes = Buffer.concat([encryptedBytes, cipher.final()]);
@@ -226,11 +226,11 @@ var aesEncrypt = function(secret, plaintext) {
226226
* @returns string 解密后的明文
227227
*/
228228
var aesDecrypt = function(secret, encryptedHex) {
229-
var secretPadded = paddingPass(secret);
229+
var secretPadded = paddingPass(secret,AESKeyLength);
230230
var aesKey = new Buffer(secretPadded, 'utf8');
231231
var iv = aesKey.slice(0, IVLength);
232232
var encryptedBuf = new Buffer(encryptedHex, 'hex');
233-
var cipher = crypto.createDecipheriv('aes-128-cbc', aesKey, iv);
233+
var cipher = crypto.createDecipheriv('aes-256-cbc', aesKey, iv);
234234
var decryptedBytes = cipher.update(encryptedBuf);
235235
decryptedBytes = Buffer.concat([decryptedBytes, cipher.final()]);
236236

@@ -298,7 +298,7 @@ var encryptText = function(plainText,listPublic){
298298
throw new ("PublicKey list is empty");
299299
}
300300
//AES encrypt
301-
var password = crypto.randomBytes(AESBlockLength);
301+
var password = crypto.randomBytes(AESKeyLength);
302302
var aesCipher = aesEncrypt(password,plainText);
303303

304304
//

proto/MultiEncrypt.proto

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//syntax = "proto3";
2+
3+
message MultiEncrypt {
4+
message HashToken{
5+
required bytes public_hash = 1;
6+
required bytes token = 2;
7+
}
8+
//DH 随机产生的公钥
9+
required bytes public_other = 1;
10+
11+
//公钥哈希与Token数组
12+
repeated HashToken hash_token_pair = 2;
13+
14+
//字符串的密文
15+
required bytes cipher = 3;
16+
}

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,11 @@ ChainsqlAPI.prototype.renameTable = function(oldName, newName) {
361361
}
362362
}
363363
ChainsqlAPI.prototype.grant = function(name, user, flags, publicKey) {
364-
if (!(name && user && flags)) throw new Error('args is not enough')
364+
if (!(name && user && flags)) throw new Error('args is not enough')
365+
if (!util.checkUserMatchPublicKey(user,publicKey)){
366+
throw new Error('Publickey does not match User')
367+
}
368+
365369
let that = this;
366370
if (that.transaction) {
367371
this.cache.push({

src/table.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Table.prototype.insert = function(raw, field) {
3333
this.query.push(raw);
3434
}
3535
if (JSON.stringify(raw).length > 512000) {
36-
throw new Error('Insert too much value,the total value of inserted must not over 1024KB')
36+
throw new Error('Insert too much value,the total value of inserted must not over 512KB')
3737
}
3838
this.exec = 'r_insert';
3939
if (this.transaction) {
@@ -177,6 +177,8 @@ function hasExtraCond(item) {
177177
}
178178

179179
Table.prototype.limit = function(limit) {
180+
if(typeof(limit) != 'number')
181+
throw new Error('limit must be a number')
180182
if (this.exec !== 'r_get')
181183
throw new Error('Object can not hava function limit');
182184

src/util.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ const keypairs = require('chainsql-keypairs');
66
const cryptoo = require('crypto');
77
const crypto = require('../lib/crypto');
88
const opType = require('./config').opType;
9+
const addressCodec = require('chainsql-address-codec');
10+
const elliptic = require('elliptic');
11+
const Secp256k1 = elliptic.ec('secp256k1');
912

13+
var AESKeyLength = 32;
1014

1115
function getFee(api) {
1216
let cushion = api._feeCushion;
@@ -30,9 +34,9 @@ function generateToken(key, secret) {
3034
var secret = secret;
3135
var token;
3236
if (!secret) {
33-
secret = cryptoo.randomBytes(8).toString('hex');
37+
secret = cryptoo.randomBytes(AESKeyLength/2).toString('hex');
3438
var keypair = keypairs.deriveKeypair(key);
35-
token = crypto.eciesEncrypt('3b2a3563a37cdf77', keypair.publicKey);
39+
token = crypto.eciesEncrypt(secret, keypair.publicKey);
3640
} else {
3741
token = crypto.eciesEncrypt(secret, key);
3842
}
@@ -162,6 +166,40 @@ function isSqlStatementTx(type){
162166
}
163167
}
164168

169+
/**
170+
* byte型转换十六进制
171+
* @param b
172+
* @returns {string}
173+
* @constructor
174+
*/
175+
const Bytes2HexString = (b)=> {
176+
let hexs = "";
177+
for (let i = 0; i < b.length; i++) {
178+
let hex = (b[i]).toString(16);
179+
if (hex.length === 1) {
180+
hexs += '0' + hex.toUpperCase();
181+
}else {
182+
hexs += hex.toUpperCase();
183+
}
184+
}
185+
return hexs;
186+
}
187+
188+
function checkUserMatchPublicKey(user,publicKey){
189+
if(user && !publicKey){
190+
return true;
191+
}
192+
var PUBLICKEY_LENGTH = 33;
193+
var ACCOUNT_PUBLIC = 35;
194+
if(publicKey.length != 2 * PUBLICKEY_LENGTH){
195+
var decoded = addressCodec.decode(publicKey, ACCOUNT_PUBLIC);
196+
var decodedPublic = decoded.slice(1,1+PUBLICKEY_LENGTH);
197+
publicKey = Bytes2HexString(decodedPublic);
198+
}
199+
var address = keypairs.deriveAddress(publicKey)
200+
return user == address;
201+
}
202+
165203
module.exports = {
166204
getFee: getFee,
167205
getSequence: getSequence,
@@ -175,5 +213,6 @@ module.exports = {
175213
generateToken: generateToken,
176214
decodeToken: decodeToken,
177215
calcFee : calcFee,
178-
isSqlStatementTx: isSqlStatementTx
216+
isSqlStatementTx: isSqlStatementTx,
217+
checkUserMatchPublicKey: checkUserMatchPublicKey
179218
}

test/server.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,26 @@ co(function*() {
3333
// });
3434

3535

36-
var cipher = crypto.eciesEncrypt("hello","02F039E54B3A0D209D348F1B2C93BE3689F2A7595DDBFB1530499D03264B87A61F");
37-
var keypair = keypairs.deriveKeypair("ssnqAfDUjc6Bkevd1Xmz5dJS5yHdz");
36+
var cipher = crypto.eciesEncrypt("hello","03B7FBF1AC149B0D297B7407CAB9636792333B8D8B8A4036B2D4DE2E6D69D435B5");
37+
var keypair = keypairs.deriveKeypair("xxHgHoRAHdGZxy5gWUdMeUK7hWrgr");
3838
var plain = crypto.eciesDecrypt(cipher,keypair.privateKey);
3939
console.log(plain);
4040

4141
//字段级加密
4242
console.log("multi encrypt test:");
43-
var listPublic = ["aBP8JEiNXr3a9nnBFDNKKzAoGNezoXzsa1N8kQAoLU5F5HrQbFvs", "aBP8EvA6tSMzCRbfsLwiFj51vDjE4jPv9Wfkta6oNXEn8TovcxaT"];
43+
var listPublic = ["cBP7JPfSVPgqGfGXVJVw168sJU5HhQfPbvDRZyriyKNeYjYLVL8M", "cBPaLRSCwtsJbz4Rq4K2NvoiDZWJyL2RnfdGv5CQ2UFWqyJ7ekHM"];
4444
var cip = yield crypto.encryptText("test",listPublic);
4545
console.log("cipher:" + cip);
46-
var text = yield crypto.decryptText(cip,"snEqBjWd2NWZK3VgiosJbfwCiLPPZ");
46+
var text = yield crypto.decryptText(cip,"xpvPjSRCtmQ3G99Pfu1VMDMd9ET3W");
4747
console.log("plain text:" + text);
48-
var text2 = yield crypto.decryptText(cip,"ssnqAfDUjc6Bkevd1Xmz5dJS5yHdz");
48+
var text2 = yield crypto.decryptText(cip,"xnHAcvtn1eVLDskhxPKNrhTsYKqde");
4949
console.log("plain text2:" + text2);
5050

5151

5252
console.log("AesPadding Test");
53-
var aesCipher = crypto.aesEncrypt("123","test");
53+
var aesCipher = crypto.aesEncrypt("abcdefg","hello,world");
5454
console.log(aesCipher);
55-
var aesDecrypted = crypto.aesDecrypt("123","EFBFBD01EFBFBDEFBFBD027CEFBFBD636C1C6522EFBFBD2FEFBFBDEFBFBD");
55+
var aesDecrypted = crypto.aesDecrypt("abcdefg",aesCipher);
5656
console.log(aesDecrypted);
5757

5858

0 commit comments

Comments
 (0)