package mac
import "github.com/google/tink/go/mac"
Package mac provides implementations of the MAC primitive.
MAC computes a tag for a given message that can be used to authenticate a
message. MAC protects data integrity as well as provides for authenticity
of the message.
Code:play
Example¶
package main
import (
"encoding/base64"
"fmt"
"log"
"github.com/google/tink/go/keyset"
"github.com/google/tink/go/mac"
)
func main() {
kh, err := keyset.NewHandle(mac.HMACSHA256Tag256KeyTemplate())
if err != nil {
log.Fatal(err)
}
// TODO: save the keyset to a safe location. DO NOT hardcode it in source code.
// Consider encrypting it with a remote key in Cloud KMS, AWS KMS or HashiCorp Vault.
// See https://github.com/google/tink/blob/master/docs/GOLANG-HOWTO.md#storing-and-loading-existing-keysets.
m, err := mac.New(kh)
if err != nil {
log.Fatal(err)
}
msg := []byte("this data needs to be authenticated")
tag, err := m.ComputeMAC(msg)
if err != nil {
log.Fatal(err)
}
if m.VerifyMAC(tag, msg); err != nil {
log.Fatal(err)
}
fmt.Printf("Message: %s\n", msg)
fmt.Printf("Authentication tag: %s\n", base64.StdEncoding.EncodeToString(tag))
}
Index ¶
- func AESCMACTag128KeyTemplate() *tinkpb.KeyTemplate
- func HMACSHA256Tag128KeyTemplate() *tinkpb.KeyTemplate
- func HMACSHA256Tag256KeyTemplate() *tinkpb.KeyTemplate
- func HMACSHA512Tag256KeyTemplate() *tinkpb.KeyTemplate
- func HMACSHA512Tag512KeyTemplate() *tinkpb.KeyTemplate
- func New(h *keyset.Handle) (tink.MAC, error)
- func NewWithKeyManager(h *keyset.Handle, km registry.KeyManager) (tink.MAC, error)
Examples ¶
Functions ¶
func AESCMACTag128KeyTemplate ¶
func AESCMACTag128KeyTemplate() *tinkpb.KeyTemplate
AESCMACTag128KeyTemplate is a KeyTemplate that generates a AES-CMAC key with the following parameters:
- Key size: 32 bytes
- Tag size: 16 bytes
func HMACSHA256Tag128KeyTemplate ¶
func HMACSHA256Tag128KeyTemplate() *tinkpb.KeyTemplate
HMACSHA256Tag128KeyTemplate is a KeyTemplate that generates a HMAC key with the following parameters:
- Key size: 32 bytes
- Tag size: 16 bytes
- Hash function: SHA256
func HMACSHA256Tag256KeyTemplate ¶
func HMACSHA256Tag256KeyTemplate() *tinkpb.KeyTemplate
HMACSHA256Tag256KeyTemplate is a KeyTemplate that generates a HMAC key with the following parameters:
- Key size: 32 bytes
- Tag size: 32 bytes
- Hash function: SHA256
func HMACSHA512Tag256KeyTemplate ¶
func HMACSHA512Tag256KeyTemplate() *tinkpb.KeyTemplate
HMACSHA512Tag256KeyTemplate is a KeyTemplate that generates a HMAC key with the following parameters:
- Key size: 64 bytes
- Tag size: 32 bytes
- Hash function: SHA512
func HMACSHA512Tag512KeyTemplate ¶
func HMACSHA512Tag512KeyTemplate() *tinkpb.KeyTemplate
HMACSHA512Tag512KeyTemplate is a KeyTemplate that generates a HMAC key with the following parameters:
- Key size: 64 bytes
- Tag size: 64 bytes
- Hash function: SHA512
func New ¶
New creates a MAC primitive from the given keyset handle.
func NewWithKeyManager ¶
NewWithKeyManager creates a MAC primitive from the given keyset handle and a custom key manager.
Deprecated: Use New.
Source Files ¶
aes_cmac_key_manager.go hmac_key_manager.go mac.go mac_factory.go mac_key_templates.go
Directories ¶
Path | Synopsis |
---|---|
mac/subtle | Package subtle provides subtle implementations of the MAC primitive. |
- Version
- v1.7.0 (latest)
- Published
- Aug 10, 2022
- Platform
- linux/amd64
- Imports
- 14 packages
- Last checked
- 3 months ago –
Tools for package owners.