Skip to content

Commit 2f16b97

Browse files
committed
Add strToBytes, bytesToStr, rc4 #8
1 parent 0f188d6 commit 2f16b97

14 files changed

Lines changed: 327 additions & 16 deletions

DOC.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,19 @@ Bubble sort implementation.
14141414
bubbleSort([2, 1]); // -> [1, 2]
14151415
```
14161416

1417+
## bytesToStr
1418+
1419+
Convert bytes to string.
1420+
1421+
|Name |Type |Desc |
1422+
|------|------|-------------|
1423+
|str |array |Bytes array |
1424+
|return|string|Result string|
1425+
1426+
```javascript
1427+
bytesToStr([108, 105, 99, 105, 97]); // -> 'licia'
1428+
```
1429+
14171430
## callbackify
14181431

14191432
Convert a function that returns a Promise to a function following the error-first callback style.
@@ -4600,6 +4613,29 @@ range(5); // -> [0, 1, 2, 3, 4]
46004613
range(0, 5, 2) // -> [0, 2, 4]
46014614
```
46024615

4616+
## rc4
4617+
4618+
RC4 symmetric encryption implementation.
4619+
4620+
### encrypt
4621+
4622+
RC4 encryption, result as base64 string.
4623+
4624+
### decrypt
4625+
4626+
RC4 decryption, pass base64 string as input.
4627+
4628+
|Name |Type |Desc |
4629+
|------|------|--------------------------------|
4630+
|key |string|Secret key |
4631+
|str |string|String to be encrypted/decrypted|
4632+
|return|string|Encrypted/decrypted string |
4633+
4634+
```javascript
4635+
rc4.encrypt('licia', 'Hello world'); // -> 'j9y2VpSfR3AdNN8='
4636+
rc4.decrypt('licia', 'j9y2VpSfR3AdNN8='); // -> 'Hello world'
4637+
```
4638+
46034639
## ready
46044640

46054641
Invoke callback when dom is ready, similar to jQuery ready.
@@ -5075,6 +5111,19 @@ String hash function using djb2.
50755111
strHash('test'); // -> 2090770981
50765112
```
50775113

5114+
## strToBytes
5115+
5116+
Convert string into bytes.
5117+
5118+
|Name |Type |Desc |
5119+
|------|------|-----------------|
5120+
|str |string|String to convert|
5121+
|return|array |Bytes array |
5122+
5123+
```javascript
5124+
strToBytes('licia'); // -> [108, 105, 99, 105, 97]
5125+
```
5126+
50785127
## stringify
50795128

50805129
JSON stringify with support for circular object, function etc.

b/bytesToStr.i18n.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## CN
2+
3+
将字节数组转换为字符串。
4+
5+
|参数名|类型|说明|
6+
|-----|----|---|
7+
|str|array|字节数组|
8+
|return|string|目标字符串|

b/bytesToStr.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Convert bytes to string.
2+
*
3+
* |Name |Type |Desc |
4+
* |------|------|-------------|
5+
* |str |array |Bytes array |
6+
* |return|string|Result string|
7+
*/
8+
9+
/* example
10+
* bytesToStr([108, 105, 99, 105, 97]); // -> 'licia'
11+
*/
12+
13+
/* module
14+
* env: all
15+
* test: all
16+
*/
17+
18+
/* typescript
19+
* export default function bytesToStr(bytes: number[]): string
20+
*/
21+
22+
function exports(bytes) {
23+
const str = [];
24+
25+
for (let i = 0, len = bytes.length; i < len; i++) {
26+
str.push(String.fromCharCode(bytes[i]));
27+
}
28+
29+
return str.join('');
30+
}

b/bytesToStr.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it('basic', function() {
2+
expect(bytesToStr([108, 105, 99, 105, 97])).to.equal('licia');
3+
});

doc/DOC_CN.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,19 @@ btoa('Hello World'); // -> 'SGVsbG8gV29ybGQ='
14091409
bubbleSort([2, 1]); // -> [1, 2]
14101410
```
14111411

1412+
## bytesToStr
1413+
1414+
将字节数组转换为字符串。
1415+
1416+
|参数名|类型|说明|
1417+
|-----|----|---|
1418+
|str|array|字节数组|
1419+
|return|string|目标字符串|
1420+
1421+
```javascript
1422+
bytesToStr([108, 105, 99, 105, 97]); // -> 'licia'
1423+
```
1424+
14121425
## callbackify
14131426

14141427
将返回 Promise 的函数转换为使用回调的函数。
@@ -4594,6 +4607,29 @@ range(5); // -> [0, 1, 2, 3, 4]
45944607
range(0, 5, 2) // -> [0, 2, 4]
45954608
```
45964609

4610+
## rc4
4611+
4612+
RC4 对称加密算法实现。
4613+
4614+
### encrypt
4615+
4616+
RC4 加密,结果表示为 base64 字符串。
4617+
4618+
### decrypt
4619+
4620+
RC4 解密,传入 base64 字符串。
4621+
4622+
|参数名|类型|说明|
4623+
|-----|----|---|
4624+
|key|string|密钥|
4625+
|str|string|源字符串|
4626+
|return|string|目标字符串|
4627+
4628+
```javascript
4629+
rc4.encrypt('licia', 'Hello world'); // -> 'j9y2VpSfR3AdNN8='
4630+
rc4.decrypt('licia', 'j9y2VpSfR3AdNN8='); // -> 'Hello world'
4631+
```
4632+
45974633
## ready
45984634

45994635
dom 准备好时调用回调函数,类似于 jQuery 的 ready 方法。
@@ -5069,6 +5105,19 @@ startWith('ab', 'a'); // -> true
50695105
strHash('test'); // -> 2090770981
50705106
```
50715107

5108+
## strToBytes
5109+
5110+
将字符串转换为字节数组。
5111+
5112+
|参数名|类型|说明|
5113+
|-----|----|---|
5114+
|str|string|目标字符串|
5115+
|return|array|字节数组|
5116+
5117+
```javascript
5118+
strToBytes('licia'); // -> [108, 105, 99, 105, 97]
5119+
```
5120+
50725121
## stringify
50735122

50745123
JSON 序列化,支持循环引用和函数。

index.json

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,12 @@
510510
"env": "all",
511511
"test": "all"
512512
},
513+
"bytesToStr": {
514+
"description": "Convert bytes to string.",
515+
"dependencies": [],
516+
"env": "all",
517+
"test": "all"
518+
},
513519
"callbackify": {
514520
"description": "Convert a function that returns a Promise to a function following the error-first callback style.",
515521
"dependencies": [
@@ -1813,7 +1819,8 @@
18131819
"md5": {
18141820
"description": "MD5 implementation.",
18151821
"dependencies": [
1816-
"utf8"
1822+
"utf8",
1823+
"strToBytes"
18171824
],
18181825
"env": "all",
18191826
"test": "all"
@@ -2157,6 +2164,17 @@
21572164
"env": "all",
21582165
"test": "all"
21592166
},
2167+
"rc4": {
2168+
"description": "RC4 symmetric encryption implementation.",
2169+
"dependencies": [
2170+
"utf8",
2171+
"base64",
2172+
"bytesToStr",
2173+
"strToBytes"
2174+
],
2175+
"env": "all",
2176+
"test": "all"
2177+
},
21602178
"ready": {
21612179
"description": "Invoke callback when dom is ready, similar to jQuery ready.",
21622180
"dependencies": [],
@@ -2421,6 +2439,12 @@
24212439
"env": "all",
24222440
"test": "all"
24232441
},
2442+
"strToBytes": {
2443+
"description": "Convert string into bytes.",
2444+
"dependencies": [],
2445+
"env": "all",
2446+
"test": "all"
2447+
},
24242448
"stringify": {
24252449
"description": "JSON stringify with support for circular object, function etc.",
24262450
"dependencies": [

m/md5.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
* export declare function md5(msg: string): string
2020
*/
2121

22-
_('utf8');
22+
_('utf8 strToBytes');
2323

2424
// https://github.com/pvorb/node-md5
2525
function exports(msg) {
26-
const bytes = stringToBytes(msg);
26+
const bytes = strToBytes(utf8.encode(msg));
2727
const m = bytesToWords(bytes);
2828
const l = bytes.length * 8;
2929

@@ -178,18 +178,6 @@ function rotl(n, b) {
178178
return (n << b) | (n >>> (32 - b));
179179
}
180180

181-
function stringToBytes(msg) {
182-
const bytes = [];
183-
184-
msg = utf8.encode(msg);
185-
186-
for (let i = 0, len = msg.length; i < len; i++) {
187-
bytes.push(msg.charCodeAt(i) & 0xff);
188-
}
189-
190-
return bytes;
191-
}
192-
193181
function bytesToWords(bytes) {
194182
const words = [];
195183

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"licia": "./bin/licia.js"
77
},
88
"scripts": {
9-
"ci": "npm run update && npm run lint && npm test",
9+
"ci": "npm run lint && npm test",
1010
"update": "licia update",
1111
"pack": "licia pack",
1212
"lint": "eslint $/*.js [a-z]/*.js",

r/rc4.i18n.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## CN
2+
3+
RC4 对称加密算法实现。
4+
5+
### encrypt
6+
7+
RC4 加密,结果表示为 base64 字符串。
8+
9+
### decrypt
10+
11+
RC4 解密,传入 base64 字符串。
12+
13+
|参数名|类型|说明|
14+
|-----|----|---|
15+
|key|string|密钥|
16+
|str|string|源字符串|
17+
|return|string|目标字符串|

r/rc4.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* RC4 symmetric encryption implementation.
2+
*
3+
* ### encrypt
4+
*
5+
* RC4 encryption, result as base64 string.
6+
*
7+
* ### decrypt
8+
*
9+
* RC4 decryption, pass base64 string as input.
10+
*
11+
* |Name |Type |Desc |
12+
* |------|------|--------------------------------|
13+
* |key |string|Secret key |
14+
* |str |string|String to be encrypted/decrypted|
15+
* |return|string|Encrypted/decrypted string |
16+
*/
17+
18+
/* example
19+
* rc4.encrypt('licia', 'Hello world'); // -> 'j9y2VpSfR3AdNN8='
20+
* rc4.decrypt('licia', 'j9y2VpSfR3AdNN8='); // -> 'Hello world'
21+
*/
22+
23+
/* module
24+
* env: all
25+
* test: all
26+
*/
27+
28+
/* typescript
29+
* export declare const rc4: {
30+
* encrypt(key: string, str: string): string
31+
* decrypt(key: string, str: string): string
32+
* }
33+
*/
34+
35+
_('utf8 base64 bytesToStr strToBytes');
36+
37+
exports = {
38+
encrypt: function(key, str) {
39+
return rc4(key, str, false);
40+
},
41+
decrypt: function(key, str) {
42+
return rc4(key, str, true);
43+
}
44+
};
45+
46+
function rc4(key, str, decrypt) {
47+
key = strToBytes(utf8.encode(key));
48+
if (!decrypt) {
49+
str = strToBytes(utf8.encode(str));
50+
} else {
51+
str = base64.decode(str);
52+
}
53+
54+
const result = [];
55+
const s = [];
56+
let j = 0;
57+
let i = 0;
58+
let x;
59+
60+
for (i = 0; i < 256; i++) {
61+
s[i] = i;
62+
}
63+
for (i = 0; i < 256; i++) {
64+
j = (j + s[i] + key[i % key.length]) % 256;
65+
x = s[i];
66+
s[i] = s[j];
67+
s[j] = x;
68+
}
69+
i = 0;
70+
j = 0;
71+
for (let y = 0, len = str.length; y < len; y++) {
72+
i = (i + 1) % 256;
73+
j = (j + s[i]) % 256;
74+
x = s[i];
75+
s[i] = s[j];
76+
s[j] = x;
77+
result.push(str[y] ^ s[(s[i] + s[j]) % 256]);
78+
}
79+
80+
return !decrypt ? base64.encode(result) : utf8.decode(bytesToStr(result));
81+
}
82+
83+
function stringToBytes(msg) {
84+
const bytes = [];
85+
86+
msg = utf8.encode(msg);
87+
88+
for (let i = 0, len = msg.length; i < len; i++) {
89+
bytes.push(msg.charCodeAt(i) & 0xff);
90+
}
91+
92+
return bytes;
93+
}

0 commit comments

Comments
 (0)