Documentation
¶
Overview ¶
Package blake2 provides a Go wrapper around an optimized, public domain implementation of BLAKE2. The cryptographic hash function BLAKE2 is an improved version of the SHA-3 finalist BLAKE. Like BLAKE or SHA-3, BLAKE2 offers the highest security, yet is fast as MD5 on 64-bit platforms and requires at least 33% less RAM than SHA-2 or SHA-3 on low-end systems.
Index ¶
Examples ¶
Constants ¶
const ( SaltSize = C.BLAKE2B_SALTBYTES PersonalSize = C.BLAKE2B_PERSONALBYTES )
Variables ¶
This section is empty.
Functions ¶
func New ¶
New returns a new custom BLAKE2b hash.
If config is nil, uses a 64-byte digest size.
Example ¶
h := New(nil) h.Write([]byte("one two three")) d := h.Sum(nil) fmt.Printf("%X", d)
Output: 5BD2901E0955770DE513E3C2397F3B7594E6BCF21F61708DF64AAECCD6BC2BE6EAE0A2CA524CCB2A7F054464B07472B9E130966D3CE4B1870E02DA788C4E33BE
Example (Personalized) ¶
h := New(&Config{ Key: []byte("sekrit"), Salt: []byte("random but public"), Personal: []byte("myAppName"), }) h.Write([]byte("one two three")) d := h.Sum(nil) fmt.Printf("%X", d)
Output: FE995AB57D24D0A0DB081BB8C1E1EB69F46A3CE5AC97FD463837C5FCAA18080688C7389449692F32D6FD53C6B7A475E52AFF5C3FBDCD253715C4F8D1333068C5
Example (Short) ¶
h := New(&Config{Size: 32}) h.Write([]byte("one two three")) d := h.Sum(nil) fmt.Printf("%X", d)
Output: 4BBA13CA5E6C7347347A331F69CCB09872E873E9FB415A2387B025712F68844B
Example (Treehash) ¶
h := New(&Config{ Tree: &Tree{ Fanout: 64, MaxDepth: 8, LeafSize: 65536, InnerHashSize: 32, NodeDepth: 3, NodeOffset: 23, IsLastNode: true, }, }) h.Write([]byte("one two three")) d := h.Sum(nil) fmt.Printf("%X", d)
Output: E86CF85D23FF3E33CCBC37F37B3A8EAE0FAE26E763FB5253F3D740DF823D47AB1273D6FFC53AD8FB15F3153F3E9F92974510975AE08ED311C68D3E4C0A3B21A6
func NewBlake2B ¶
NewBlake2B returns a new 512-bit BLAKE2B hash.
Example ¶
h := NewBlake2B() h.Write([]byte("one two three")) d := h.Sum(nil) fmt.Printf("%X", d)
Output: 5BD2901E0955770DE513E3C2397F3B7594E6BCF21F61708DF64AAECCD6BC2BE6EAE0A2CA524CCB2A7F054464B07472B9E130966D3CE4B1870E02DA788C4E33BE
func NewKeyedBlake2B ¶
NewKeyedBlake2B returns a new 512-bit BLAKE2B hash with the given secret key.
Example ¶
h := NewKeyedBlake2B([]byte("my secret")) h.Write([]byte("one two three")) d := h.Sum(nil) fmt.Printf("%X", d)
Output: FC182724DC024B95F62E606859AC806E4EDCA09A927F6BC8BCCD07DADE3E4F26FC9D041661407527AADEF517A173E19BAB5C389217C29A08BE9731AEC83C02C3
Types ¶
type Config ¶
type Config struct { // Digest byte length, in the range [1, 64]. If 0, default size of 64 bytes is used. Size uint8 // Key is up to 64 arbitrary bytes, for keyed hashing mode. Can be nil. Key []byte // Salt is up to 16 arbitrary bytes, used to randomize the hash. Can be nil. Salt []byte // Personal is up to 16 arbitrary bytes, used to make the hash // function unique for each application. Can be nil. Personal []byte // Parameters for tree hashing. Set to nil to use default // sequential mode. Tree *Tree }
Config contains parameters for the hash function that affect its output.
type Tree ¶
type Tree struct { // Fanout: how many children each tree node has. 0 for unlimited. // 1 means sequential mode. Fanout uint8 // Maximal depth of the tree. Beyond this height, nodes are just // added to the root of the tree. 255 for unlimited. 1 means // sequential mode. MaxDepth uint8 // Leaf maximal byte length, how much data each leaf summarizes. 0 // for unlimited or sequential mode. LeafSize uint32 // Depth of this node. 0 for leaves or sequential mode. NodeDepth uint8 // Offset of this node within this level of the tree. 0 for the // first, leftmost, leaf, or sequential mode. NodeOffset uint64 // Inner hash byte length, in the range [0, 64]. 0 for sequential // mode. InnerHashSize uint8 // IsLastNode indicates this node is the last, rightmost, node of // a level of the tree. IsLastNode bool }
Tree contains parameters for tree hashing. Each node in the tree can be hashed concurrently, and incremental changes can be done in a Merkle tree fashion.