https://github.com/geod24/bitblob
A small value type to represent hashes
https://github.com/geod24/bitblob
Last synced: 4 months ago
JSON representation
A small value type to represent hashes
- Host: GitHub
- URL: https://github.com/geod24/bitblob
- Owner: Geod24
- License: mit
- Created: 2018-12-07T09:26:44.000Z (over 7 years ago)
- Default Branch: v2.x.x
- Last Pushed: 2024-05-23T22:43:50.000Z (about 2 years ago)
- Last Synced: 2025-03-25T02:40:35.769Z (about 1 year ago)
- Language: D
- Size: 26.4 KB
- Stars: 1
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# BitBlob
[](https://code.dlang.org/packages/bitblob)
[](https://codecov.io/gh/Geod24/bitblob)
A lightweight wrapper to represent hashes as value types.
## Example
```D
/// Alias for a 256 bits / 32 byte hash type
alias Hash = BitBlob!32;
/// Used in the following tests
enum BTCGenesisStr = `0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f`;
// Most functions are anotated as `nothrow`/`@nogc`/`pure`/`@safe`,
// except for the two `toString` overloads
@nogc @safe pure nothrow unittest
{
import std.digest.sha;
// Can be initialized from an `ubyte[32]`
// (or `ubyte[]` of length 32)
const Hash fromSha = sha256Of("Hello World");
// Or from a string
const Hash genesis = BTCGenesisStr;
assert(!genesis.isNull());
assert(Hash.init.isNull());
ubyte[5] empty;
assert(Hash.init < genesis);
assert(genesis[0 .. 5] == empty);
}
// This cannot be nothrow/@nogc because format is not allocated as such
// However, Bitblob has tests that it does not allocate
unittest
{
// This does not allocate
char[Hash.StringBufferSize] buff;
const Hash genesis = BTCGenesisStr;
formattedWrite(buff[], "%s", genesis);
assert(buff[] == BTCGenesisStr);
}
```
## Note on design
`BitBlob` is intended for convenient representation
of fully computed / finalized hashes.
String representation, comparison, and seamless conversion
from string or binary representation are the focus.
Byte-level manipulation of the underlying data is not
prefered, although possible through `opSlice`.