github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/hash/blake2b.go (about)

     1  // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
     2  //
     3  // Copyright 2020 Stafi Protocol
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package hash
    18  
    19  import (
    20  	"hash"
    21  
    22  	"golang.org/x/crypto/blake2b"
    23  )
    24  
    25  type blake2b128Concat struct {
    26  	hasher hash.Hash
    27  	data   []byte
    28  }
    29  
    30  // NewBlake2b128Concat returns an instance of blake2b concat hasher
    31  func NewBlake2b128Concat(k []byte) (hash.Hash, error) {
    32  	h, err := blake2b.New(16, k)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return &blake2b128Concat{hasher: h, data: k}, nil
    37  }
    38  
    39  // Write (via the embedded io.Writer interface) adds more data to the running hash.
    40  func (bc *blake2b128Concat) Write(p []byte) (n int, err error) {
    41  	bc.data = append(bc.data, p...)
    42  	return bc.hasher.Write(p)
    43  }
    44  
    45  // Sum appends the current hash to b and returns the resulting slice.
    46  // It does not change the underlying hash state.
    47  func (bc *blake2b128Concat) Sum(b []byte) []byte {
    48  	return append(bc.hasher.Sum(b), bc.data...)
    49  }
    50  
    51  // Reset resets the Hash to its initial state.
    52  func (bc *blake2b128Concat) Reset() {
    53  	bc.data = nil
    54  	bc.hasher.Reset()
    55  }
    56  
    57  // Size returns the number of bytes Sum will return.
    58  func (bc *blake2b128Concat) Size() int {
    59  	return len(bc.Sum(nil))
    60  }
    61  
    62  // BlockSize returns the hash's underlying block size.
    63  // The Write method must be able to accept any amount
    64  // of data, but it may operate more efficiently if all writes
    65  // are a multiple of the block size.
    66  func (bc *blake2b128Concat) BlockSize() int {
    67  	return bc.hasher.BlockSize()
    68  }
    69  
    70  // NewBlake2b128 returns blake2b-128 hasher
    71  func NewBlake2b128(k []byte) (hash.Hash, error) {
    72  	return blake2b.New(16, k)
    73  }
    74  
    75  // NewBlake2b256 returns blake2b-256 hasher
    76  func NewBlake2b256(k []byte) (hash.Hash, error) {
    77  	return blake2b.New256(k)
    78  }