git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/crypto/mac.go (about)

     1  package crypto
     2  
     3  import (
     4  	"errors"
     5  
     6  	"golang.org/x/crypto/blake2b"
     7  )
     8  
     9  // Mac use the blake2b funciton in MAC mode
    10  func Mac(key, data []byte, macSize uint8) ([]byte, error) {
    11  	if macSize < 1 || macSize > 64 {
    12  		return nil, errors.New("crypto: macSize must be between 1 and 64")
    13  	}
    14  
    15  	blake2bHash, err := blake2b.New(int(macSize), key)
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  
    20  	blake2bHash.Write(data)
    21  	return blake2bHash.Sum(nil), nil
    22  }