Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

Commit

Permalink
调整代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Srar committed Jul 1, 2018
1 parent 678c084 commit 845d842
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
19 changes: 5 additions & 14 deletions test/shadowsocks/crypto/CryptoMethods/RC4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,29 @@ export default class RC4 implements ISSCryptoMethod {
}

public encryptData(data: Buffer): Buffer {
if (this.isFirstEncryptData) {
if (this.isFirstEncryptData) {
this.isFirstEncryptData = false;
this.cryptoKeyIV.iv = crypto.randomBytes(RC4.ivLength);
this.encryptProcess = crypto.createCipheriv("rc4", this.cryptoKeyIV.key , "");
return Buffer.concat([this.cryptoKeyIV.iv, this.encryptProcess.update(data)]);
}
// tslint:disable-next-line:align
return this.encryptProcess.update(data);
}

public decryptData(data: Buffer): Buffer {
if (this.isFirstDecryptData) {
public decryptData(data: Buffer): Buffer {
if (this.isFirstDecryptData) {
this.isFirstDecryptData = false;
const decryptIV: Buffer = data.slice(0, RC4.ivLength);
this.decryptProcess = crypto.createDecipheriv("rc4", this.cryptoKeyIV.key, "");
return this.decryptProcess.update(data.slice(RC4.ivLength));
}
// tslint:disable-next-line:align
return this.decryptProcess.update(data);
}

public encryptDataWithoutStream(data: Buffer) {
this.cryptoKeyIV.iv = crypto.randomBytes(RC4.ivLength);
// const rc4Process: Buffer = CryptoTools.generateRc4Md5KeyByKV(this.cryptoKeyIV);
const encryptProcess = crypto.createCipheriv("rc4", this.cryptoKeyIV.key , "");
return Buffer.concat([this.cryptoKeyIV.iv, encryptProcess.update(data)]);
return Buffer.concat([encryptProcess.update(data), encryptProcess.final()]);
}

public decryptDataWithoutStream(data: Buffer) {
const decryptIV: Buffer = data.slice(0, RC4.ivLength);
const decryptProcess = crypto.createDecipheriv("rc4", this.cryptoKeyIV.key, "");
return decryptProcess.update(data.slice(RC4.ivLength));
return decryptProcess.update(data);
}

public getCryptoName(): string {
Expand Down
6 changes: 2 additions & 4 deletions test/shadowsocks/crypto/CryptoMethods/RC4MD5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,24 @@ export default class RC4MD5 implements ISSCryptoMethod {
}

public encryptData(data: Buffer): Buffer {
if (this.isFirstEncryptData) {
if (this.isFirstEncryptData) {
this.isFirstEncryptData = false;
this.cryptoKeyIV.iv = crypto.randomBytes(RC4MD5.ivLength);
const rc4Process: Buffer = CryptoTools.generateRc4Md5KeyByKV(this.cryptoKeyIV);
this.encryptProcess = crypto.createCipheriv("rc4", rc4Process , "");
return Buffer.concat([this.cryptoKeyIV.iv, this.encryptProcess.update(data)]);
}
// tslint:disable-next-line:align
return this.encryptProcess.update(data);
}

public decryptData(data: Buffer): Buffer {
if (this.isFirstDecryptData) {
if (this.isFirstDecryptData) {
this.isFirstDecryptData = false;
const decryptIV: Buffer = data.slice(0, RC4MD5.ivLength);
const rc4Process: Buffer = CryptoTools.generateRc4Md5KeyByKV({ key: this.cryptoKeyIV.key, iv: decryptIV });
this.decryptProcess = crypto.createDecipheriv("rc4", rc4Process, "");
return this.decryptProcess.update(data.slice(RC4MD5.ivLength));
}
// tslint:disable-next-line:align
return this.decryptProcess.update(data);
}

Expand Down
2 changes: 1 addition & 1 deletion test/shadowsocks/crypto/CryptoTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class CryptoTools {
}
const hashBuffer: Buffer = Buffer.concat(hashBuffers);
const key: Buffer = hashBuffer.slice(0, keyLength);
const iv: Buffer = hashBuffer.slice(keyLength, keyLength + ivLength);
const iv: Buffer = crypto.randomBytes(ivLength);
return {
key,
iv,
Expand Down

0 comments on commit 845d842

Please sign in to comment.