UNPKG

11.2 kBTypeScriptView Raw
1/// <reference types="node" />
2/**
3 * Ported from {@link https://github.com/google/crc32c/blob/21fc8ef30415a635e7351ffa0e5d5367943d4a94/src/crc32c_portable.cc#L16-L59 github.com/google/crc32c}
4 */
5declare const CRC32C_EXTENSIONS: readonly [0, 4067132163, 3778769143, 324072436, 3348797215, 904991772, 648144872, 3570033899, 2329499855, 2024987596, 1809983544, 2575936315, 1296289744, 3207089363, 2893594407, 1578318884, 274646895, 3795141740, 4049975192, 51262619, 3619967088, 632279923, 922689671, 3298075524, 2592579488, 1760304291, 2075979607, 2312596564, 1562183871, 2943781820, 3156637768, 1313733451, 549293790, 3537243613, 3246849577, 871202090, 3878099393, 357341890, 102525238, 4101499445, 2858735121, 1477399826, 1264559846, 3107202533, 1845379342, 2677391885, 2361733625, 2125378298, 820201905, 3263744690, 3520608582, 598981189, 4151959214, 85089709, 373468761, 3827903834, 3124367742, 1213305469, 1526817161, 2842354314, 2107672161, 2412447074, 2627466902, 1861252501, 1098587580, 3004210879, 2688576843, 1378610760, 2262928035, 1955203488, 1742404180, 2511436119, 3416409459, 969524848, 714683780, 3639785095, 205050476, 4266873199, 3976438427, 526918040, 1361435347, 2739821008, 2954799652, 1114974503, 2529119692, 1691668175, 2005155131, 2247081528, 3690758684, 697762079, 986182379, 3366744552, 476452099, 3993867776, 4250756596, 255256311, 1640403810, 2477592673, 2164122517, 1922457750, 2791048317, 1412925310, 1197962378, 3037525897, 3944729517, 427051182, 170179418, 4165941337, 746937522, 3740196785, 3451792453, 1070968646, 1905808397, 2213795598, 2426610938, 1657317369, 3053634322, 1147748369, 1463399397, 2773627110, 4215344322, 153784257, 444234805, 3893493558, 1021025245, 3467647198, 3722505002, 797665321, 2197175160, 1889384571, 1674398607, 2443626636, 1164749927, 3070701412, 2757221520, 1446797203, 137323447, 4198817972, 3910406976, 461344835, 3484808360, 1037989803, 781091935, 3705997148, 2460548119, 1623424788, 1939049696, 2180517859, 1429367560, 2807687179, 3020495871, 1180866812, 410100952, 3927582683, 4182430767, 186734380, 3756733383, 763408580, 1053836080, 3434856499, 2722870694, 1344288421, 1131464017, 2971354706, 1708204729, 2545590714, 2229949006, 1988219213, 680717673, 3673779818, 3383336350, 1002577565, 4010310262, 493091189, 238226049, 4233660802, 2987750089, 1082061258, 1395524158, 2705686845, 1972364758, 2279892693, 2494862625, 1725896226, 952904198, 3399985413, 3656866545, 731699698, 4283874585, 222117402, 510512622, 3959836397, 3280807620, 837199303, 582374963, 3504198960, 68661723, 4135334616, 3844915500, 390545967, 1230274059, 3141532936, 2825850620, 1510247935, 2395924756, 2091215383, 1878366691, 2644384480, 3553878443, 565732008, 854102364, 3229815391, 340358836, 3861050807, 4117890627, 119113024, 1493875044, 2875275879, 3090270611, 1247431312, 2660249211, 1828433272, 2141937292, 2378227087, 3811616794, 291187481, 34330861, 4032846830, 615137029, 3603020806, 3314634738, 939183345, 1776939221, 2609017814, 2295496738, 2058945313, 2926798794, 1545135305, 1330124605, 3173225534, 4084100981, 17165430, 307568514, 3762199681, 888469610, 3332340585, 3587147933, 665062302, 2042050490, 2346497209, 2559330125, 1793573966, 3190661285, 1279665062, 1595330642, 2910671697];
6declare const CRC32C_EXTENSION_TABLE: Int32Array;
7/** An interface for CRC32C hashing and validation */
8interface CRC32CValidator {
9 /**
10 * A method returning the CRC32C as a base64-encoded string.
11 *
12 * @example
13 * Hashing the string 'data' should return 'rth90Q=='
14 *
15 * ```js
16 * const buffer = Buffer.from('data');
17 * crc32c.update(buffer);
18 * crc32c.toString(); // 'rth90Q=='
19 * ```
20 **/
21 toString: () => string;
22 /**
23 * A method validating a base64-encoded CRC32C string.
24 *
25 * @example
26 * Should return `true` if the value matches, `false` otherwise
27 *
28 * ```js
29 * const buffer = Buffer.from('data');
30 * crc32c.update(buffer);
31 * crc32c.validate('DkjKuA=='); // false
32 * crc32c.validate('rth90Q=='); // true
33 * ```
34 */
35 validate: (value: string) => boolean;
36 /**
37 * A method for passing `Buffer`s for CRC32C generation.
38 *
39 * @example
40 * Hashing buffers from 'some ' and 'text\n'
41 *
42 * ```js
43 * const buffer1 = Buffer.from('some ');
44 * crc32c.update(buffer1);
45 *
46 * const buffer2 = Buffer.from('text\n');
47 * crc32c.update(buffer2);
48 *
49 * crc32c.toString(); // 'DkjKuA=='
50 * ```
51 */
52 update: (data: Buffer) => void;
53}
54/** A function that generates a CRC32C Validator */
55interface CRC32CValidatorGenerator {
56 /** Should return a new, ready-to-use `CRC32CValidator` */
57 (): CRC32CValidator;
58}
59declare const CRC32C_DEFAULT_VALIDATOR_GENERATOR: CRC32CValidatorGenerator;
60declare const CRC32C_EXCEPTION_MESSAGES: {
61 readonly INVALID_INIT_BASE64_RANGE: (l: number) => string;
62 readonly INVALID_INIT_BUFFER_LENGTH: (l: number) => string;
63 readonly INVALID_INIT_INTEGER: (l: number) => string;
64};
65declare class CRC32C implements CRC32CValidator {
66 #private;
67 /**
68 * Constructs a new `CRC32C` object.
69 *
70 * Reconstruction is recommended via the `CRC32C.from` static method.
71 *
72 * @param initialValue An initial CRC32C value - a signed 32-bit integer.
73 */
74 constructor(initialValue?: number);
75 /**
76 * Calculates a CRC32C from a provided buffer.
77 *
78 * Implementation inspired from:
79 * - {@link https://github.com/google/crc32c/blob/21fc8ef30415a635e7351ffa0e5d5367943d4a94/src/crc32c_portable.cc github.com/google/crc32c}
80 * - {@link https://github.com/googleapis/python-crc32c/blob/a595e758c08df445a99c3bf132ee8e80a3ec4308/src/google_crc32c/python.py github.com/googleapis/python-crc32c}
81 * - {@link https://github.com/googleapis/java-storage/pull/1376/files github.com/googleapis/java-storage}
82 *
83 * @param data The `Buffer` to generate the CRC32C from
84 */
85 update(data: Buffer): void;
86 /**
87 * Validates a provided input to the current CRC32C value.
88 *
89 * @param input A Buffer, `CRC32C`-compatible object, base64-encoded data (string), or signed 32-bit integer
90 */
91 validate(input: Buffer | CRC32CValidator | string | number): boolean;
92 /**
93 * Returns a `Buffer` representation of the CRC32C value
94 */
95 toBuffer(): Buffer;
96 /**
97 * Returns a JSON-compatible, base64-encoded representation of the CRC32C value.
98 *
99 * See {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify `JSON#stringify`}
100 */
101 toJSON(): string;
102 /**
103 * Returns a base64-encoded representation of the CRC32C value.
104 *
105 * See {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString `Object#toString`}
106 */
107 toString(): string;
108 /**
109 * Returns the `number` representation of the CRC32C value as a signed 32-bit integer
110 *
111 * See {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf `Object#valueOf`}
112 */
113 valueOf(): number;
114 static readonly CRC32C_EXTENSIONS: readonly [0, 4067132163, 3778769143, 324072436, 3348797215, 904991772, 648144872, 3570033899, 2329499855, 2024987596, 1809983544, 2575936315, 1296289744, 3207089363, 2893594407, 1578318884, 274646895, 3795141740, 4049975192, 51262619, 3619967088, 632279923, 922689671, 3298075524, 2592579488, 1760304291, 2075979607, 2312596564, 1562183871, 2943781820, 3156637768, 1313733451, 549293790, 3537243613, 3246849577, 871202090, 3878099393, 357341890, 102525238, 4101499445, 2858735121, 1477399826, 1264559846, 3107202533, 1845379342, 2677391885, 2361733625, 2125378298, 820201905, 3263744690, 3520608582, 598981189, 4151959214, 85089709, 373468761, 3827903834, 3124367742, 1213305469, 1526817161, 2842354314, 2107672161, 2412447074, 2627466902, 1861252501, 1098587580, 3004210879, 2688576843, 1378610760, 2262928035, 1955203488, 1742404180, 2511436119, 3416409459, 969524848, 714683780, 3639785095, 205050476, 4266873199, 3976438427, 526918040, 1361435347, 2739821008, 2954799652, 1114974503, 2529119692, 1691668175, 2005155131, 2247081528, 3690758684, 697762079, 986182379, 3366744552, 476452099, 3993867776, 4250756596, 255256311, 1640403810, 2477592673, 2164122517, 1922457750, 2791048317, 1412925310, 1197962378, 3037525897, 3944729517, 427051182, 170179418, 4165941337, 746937522, 3740196785, 3451792453, 1070968646, 1905808397, 2213795598, 2426610938, 1657317369, 3053634322, 1147748369, 1463399397, 2773627110, 4215344322, 153784257, 444234805, 3893493558, 1021025245, 3467647198, 3722505002, 797665321, 2197175160, 1889384571, 1674398607, 2443626636, 1164749927, 3070701412, 2757221520, 1446797203, 137323447, 4198817972, 3910406976, 461344835, 3484808360, 1037989803, 781091935, 3705997148, 2460548119, 1623424788, 1939049696, 2180517859, 1429367560, 2807687179, 3020495871, 1180866812, 410100952, 3927582683, 4182430767, 186734380, 3756733383, 763408580, 1053836080, 3434856499, 2722870694, 1344288421, 1131464017, 2971354706, 1708204729, 2545590714, 2229949006, 1988219213, 680717673, 3673779818, 3383336350, 1002577565, 4010310262, 493091189, 238226049, 4233660802, 2987750089, 1082061258, 1395524158, 2705686845, 1972364758, 2279892693, 2494862625, 1725896226, 952904198, 3399985413, 3656866545, 731699698, 4283874585, 222117402, 510512622, 3959836397, 3280807620, 837199303, 582374963, 3504198960, 68661723, 4135334616, 3844915500, 390545967, 1230274059, 3141532936, 2825850620, 1510247935, 2395924756, 2091215383, 1878366691, 2644384480, 3553878443, 565732008, 854102364, 3229815391, 340358836, 3861050807, 4117890627, 119113024, 1493875044, 2875275879, 3090270611, 1247431312, 2660249211, 1828433272, 2141937292, 2378227087, 3811616794, 291187481, 34330861, 4032846830, 615137029, 3603020806, 3314634738, 939183345, 1776939221, 2609017814, 2295496738, 2058945313, 2926798794, 1545135305, 1330124605, 3173225534, 4084100981, 17165430, 307568514, 3762199681, 888469610, 3332340585, 3587147933, 665062302, 2042050490, 2346497209, 2559330125, 1793573966, 3190661285, 1279665062, 1595330642, 2910671697];
115 static readonly CRC32C_EXTENSION_TABLE: Int32Array;
116 /**
117 * Generates a `CRC32C` from a compatible buffer format.
118 *
119 * @param value 4-byte `ArrayBufferView`/`Buffer`/`TypedArray`
120 */
121 private static fromBuffer;
122 /**
123 * Generates a `CRC32C` from 4-byte base64-encoded data (string).
124 *
125 * @param value 4-byte base64-encoded data (string)
126 */
127 private static fromString;
128 /**
129 * Generates a `CRC32C` from a safe, unsigned 32-bit integer.
130 *
131 * @param value an unsigned 32-bit integer
132 */
133 private static fromNumber;
134 /**
135 * Generates a `CRC32C` from a variety of compatable types.
136 *
137 * @param value A number, 4-byte `ArrayBufferView`/`Buffer`/`TypedArray`, or 4-byte base64-encoded data (string)
138 */
139 static from(value: ArrayBuffer | ArrayBufferView | CRC32CValidator | string | number): CRC32C;
140}
141export { CRC32C, CRC32C_DEFAULT_VALIDATOR_GENERATOR, CRC32C_EXCEPTION_MESSAGES, CRC32C_EXTENSIONS, CRC32C_EXTENSION_TABLE, CRC32CValidator, CRC32CValidatorGenerator, };