File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2052,6 +2052,20 @@ CRC16 implementation.
20522052crc16 (' 1234567890' ).toString (16 ); // -> 'c57a'
20532053```
20542054
2055+ ## crc32
2056+
2057+ CRC32 implementation.
2058+
2059+ | Name | Type | Desc |
2060+ | ----------| ------------------------------------| ---------------------|
2061+ | input | string Buffer ArrayBuffer Uint8Array| Data to calculate |
2062+ | [ previous] | number | Previous CRC32 result|
2063+ | return | number | CRC16 result |
2064+
2065+ ``` javascript
2066+ crc32 (' 1234567890' ).toString (16 ); // -> '261daee5'
2067+ ```
2068+
20552069## crc8
20562070
20572071CRC8 implementation.
Original file line number Diff line number Diff line change @@ -2047,6 +2047,20 @@ CRC16 算法实现。
20472047crc16 (' 1234567890' ).toString (16 ); // -> 'c57a'
20482048```
20492049
2050+ ## crc32
2051+
2052+ CRC32 算法实现。
2053+
2054+ | 参数名| 类型| 说明|
2055+ | -----| ----| ---|
2056+ | input| string Buffer ArrayBuffer Uint8Array| 信息码|
2057+ | [ previous] | number| 用于累积计算的 CRC32 校验码|
2058+ | 返回值| number| CRC32 校验码|
2059+
2060+ ``` javascript
2061+ crc32 (' 1234567890' ).toString (16 ); // -> '261daee5'
2062+ ```
2063+
20502064## crc8
20512065
20522066CRC8 算法实现。
Original file line number Diff line number Diff line change 12561256 " browser"
12571257 ]
12581258 },
1259+ "crc32" : {
1260+ "description" : " CRC32 implementation." ,
1261+ "dependencies" : [
1262+ " crc1"
1263+ ],
1264+ "env" : [
1265+ " node" ,
1266+ " browser" ,
1267+ " miniprogram"
1268+ ],
1269+ "test" : [
1270+ " node" ,
1271+ " browser"
1272+ ]
1273+ },
12591274 "crc8" : {
12601275 "description" : " CRC8 implementation." ,
12611276 "dependencies" : [
Original file line number Diff line number Diff line change 1+ ## CN
2+
3+ CRC32 算法实现。
4+
5+ | 参数名| 类型| 说明|
6+ | -----| ----| ---|
7+ | input| string Buffer ArrayBuffer Uint8Array| 信息码|
8+ | [ previous] | number| 用于累积计算的 CRC32 校验码|
9+ | 返回值| number| CRC32 校验码|
Original file line number Diff line number Diff line change 1+ /* CRC32 implementation.
2+ *
3+ * |Name |Type |Desc |
4+ * |----------|------------------------------------|---------------------|
5+ * |input |string Buffer ArrayBuffer Uint8Array|Data to calculate |
6+ * |[previous]|number |Previous CRC32 result|
7+ * |return |number |CRC16 result |
8+ */
9+
10+ /* example
11+ * crc32('1234567890').toString(16); // -> '261daee5'
12+ */
13+
14+ /* module
15+ * env: all
16+ * test: all
17+ */
18+
19+ /* typescript
20+ * export declare function crc32(
21+ * input: string | Buffer | ArrayBuffer | Uint8Array,
22+ * previous?: number
23+ * ): number;
24+ */
25+
26+ _ ( 'crc1' ) ;
27+
28+ let TABLE = [ ] ;
29+
30+ for ( let n = 0 ; n < 256 ; n ++ ) {
31+ let c = n ;
32+ for ( let k = 0 ; k < 8 ; k ++ ) {
33+ if ( c & 1 ) {
34+ c = 0xedb88320 ^ ( c >>> 1 ) ;
35+ } else {
36+ c = c >>> 1 ;
37+ }
38+ }
39+ TABLE [ n ] = c >>> 0 ;
40+ }
41+
42+ if ( typeof Int32Array !== 'undefined' ) TABLE = new Int32Array ( TABLE ) ;
43+
44+ exports = function ( input , previous ) {
45+ return exports . signed ( input , previous ) >>> 0 ;
46+ } ;
47+
48+ exports . signed = function ( input , previous ) {
49+ input = crc1 . transInput ( input ) ;
50+
51+ let crc = previous === 0 ? 0 : ~ ~ previous ^ - 1 ;
52+
53+ for ( let i = 0 , len = input . length ; i < len ; i ++ ) {
54+ const byte = input [ i ] ;
55+ crc = TABLE [ ( crc ^ byte ) & 0xff ] ^ ( crc >>> 8 ) ;
56+ }
57+
58+ return crc ^ - 1 ;
59+ } ;
Original file line number Diff line number Diff line change 1+ it ( 'basic' , ( ) => {
2+ expect ( crc32 ( '1234567890' ) . toString ( 16 ) ) . to . equal ( '261daee5' ) ;
3+ } ) ;
You can’t perform that action at this time.
0 commit comments