Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/orvn/crc32-js
A JS CRC32 and CRC16 checksum implementation that matches C and PHP
https://github.com/orvn/crc32-js
checksum hash hashing-algorithm
Last synced: 12 days ago
JSON representation
A JS CRC32 and CRC16 checksum implementation that matches C and PHP
- Host: GitHub
- URL: https://github.com/orvn/crc32-js
- Owner: orvn
- License: apache-2.0
- Created: 2023-05-06T05:09:12.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-08-07T23:57:05.000Z (over 1 year ago)
- Last Synced: 2024-12-16T10:46:18.503Z (22 days ago)
- Topics: checksum, hash, hashing-algorithm
- Language: JavaScript
- Homepage:
- Size: 730 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CRC32 (and CRC16) in JS
A better Javascript CRC checksum or hashing implementation that yields the same result as C and PHP CRC functions.
## Purpose
This package was created because other JavaScript CRC functions either tend to be inefficient, or don't match the CRC function output from implementations in C, or in PHP.
Included is a 32-bit and a 16-bit CRC (cyclic redundancy checksum) function. This function is often used to check for data integregity. It can also be used as a concise hash, however there are some chances for collision at scale.
## Usage
After installing and importing the package (npm and yarn entries coming soon), it can be used as follows
```js
crc32(str, [usePolynomialDivision], [format])
```Include a string to hash, and optionally elect to use polynomial division, which is set to `false` by default. Instead of polynomial division, the default is to use a pre-calculated lookup table, which is faster. This is also the implementation strategy used within the C and PHP source code.
The last option defaults to `true`, making the output a hexadecimal value. If set to `false` the output will be in decimal format.
## Collision resistance
CRC32 might be attractive as a hashing function because of its concise nature. However, collisions _should_ be expected at scale. For this reason it can be used as a hashing function at small scale, or as an effective checksum function.
At first it might seem that the chance of collision is low, because CRC32 results in fixed length 32-bit values. However, the birthday paradox should be considered when checking for the probability of collision between any two values, as a set grows.
### $`P = 1 - \frac{k!}{k^n (k-n)!}`$
where
- `n` is the size of the set
- `k` is the total number of possible hashes in the algorithm (2^32)
- `P` is the probability of a collisionApplying this, if we have a set size of 1000 different strings, the probability of a collision between two of them is `0.01164%`, or `1 in 8600`. Clearly, this is a good function for checksums, but not an ideal function for most applications of hash functions.
## Roadmap
Other features that are coming soon include the following:
- Argument to toggle between CRC32a and CRC32b
- CRC16 implementation (currently incomplete)
- Docs that explain how CRC32 works in more detail