Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/OpenSystemsLab/xxhash.nim
xxhash wrapper for Nim
https://github.com/OpenSystemsLab/xxhash.nim
Last synced: 3 days ago
JSON representation
xxhash wrapper for Nim
- Host: GitHub
- URL: https://github.com/OpenSystemsLab/xxhash.nim
- Owner: OpenSystemsLab
- License: mit
- Created: 2015-07-27T05:09:16.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-09-17T02:16:32.000Z (4 months ago)
- Last Synced: 2024-09-29T15:04:22.012Z (4 months ago)
- Language: Nim
- Size: 26.4 KB
- Stars: 14
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-nim - xxhash.nim - A wrapper for the xxhash hashing library in Nim. (Algorithms / Cryptography)
README
# xxhash.nim
**xxhash wrapper for Nim**
This is a wrapper for the `xxhash` hashing library in Nim, which provides fast, non-cryptographic hashing for various use cases such as file integrity checks, data storage, and more.## Features
- Supports `XXH32`, `XXH64`, and `XXH128` hashing algorithms.
- Designed to work seamlessly with the Nim language.
- Efficient and fast hashing suitable for performance-sensitive applications.## Installation
To use `xxhash.nim`, install the library using Nimble:```shell
nimble install xxhash
```Alternatively, you can clone the repository from the official source and add it to your project manually.
## Basic Usage
Below is a simple example of how to use the `xxhash` library in a Nim project.### Importing the Module
```nim
import xxhash
```### Hashing a String
To compute a 32-bit hash of a string using `XXH32`:```nim
let hash32 = XXH32("Hello, world!")
echo hash32
```To compute a 64-bit hash using `XXH64`:
```nim
let hash64 = XXH64("Hello, world!")
echo hash64
```For larger data or more security, use `XXH128`:
```nim
let hash128 = XXH3_128bits("Hello, world!")
echo hash128
```### Hashing a File
To hash a file, you can use:```nim
import oslet fileContent = readFile("path/to/file")
let fileHash = XXH64(fileContent)
echo fileHash
```### Streaming Hash Computation
For large files or streams, the xxhash library provides stateful hashing that allows hashing in chunks. Here's an example using `XXH64`:```nim
var state = newXxh64()# Process in chunks
state.update("Hello, ")
state.update("world!")let finalHash = state.digest()
echo finalHash# Important: Free the state after use in Nim 1.x.x
state.free()
```## Note for Nim version 1.x.x
In Nim versions older than 2.0.0, you should manually call `state.free()` to deallocate resources because Nim does not automatically call destructors (`=destroy`). This step is crucial to avoid memory leaks when using the stateful API.## Performance Considerations
- `xxhash` is designed to be very fast and is optimized for bulk data processing.
- `XXH128` is slightly slower but provides more robust hashing for larger datasets.## Additional Information
This wrapper supports both 32-bit and 64-bit systems, ensuring compatibility across various platforms.## Credits
- This module utilizes [@rockcavera](https://github.com/rockcavera)'s [`nint128`](https://github.com/rockcavera/nim-nint128) for XXH128 computation.## License
This project is distributed under the MIT License.---
*This README was generated by [ChatGPT](https://openai.com/chatgpt).*