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.

Example

Code:play 

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

Examples

Functions

func AESCMACTag128KeyTemplate

func AESCMACTag128KeyTemplate() *tinkpb.KeyTemplate

AESCMACTag128KeyTemplate is a KeyTemplate that generates a AES-CMAC key with the following parameters:

func HMACSHA256Tag128KeyTemplate

func HMACSHA256Tag128KeyTemplate() *tinkpb.KeyTemplate

HMACSHA256Tag128KeyTemplate is a KeyTemplate that generates a HMAC key with the following parameters:

func HMACSHA256Tag256KeyTemplate

func HMACSHA256Tag256KeyTemplate() *tinkpb.KeyTemplate

HMACSHA256Tag256KeyTemplate is a KeyTemplate that generates a HMAC key with the following parameters:

func HMACSHA512Tag256KeyTemplate

func HMACSHA512Tag256KeyTemplate() *tinkpb.KeyTemplate

HMACSHA512Tag256KeyTemplate is a KeyTemplate that generates a HMAC key with the following parameters:

func HMACSHA512Tag512KeyTemplate

func HMACSHA512Tag512KeyTemplate() *tinkpb.KeyTemplate

HMACSHA512Tag512KeyTemplate is a KeyTemplate that generates a HMAC key with the following parameters:

func New

func New(h *keyset.Handle) (tink.MAC, error)

New creates a MAC primitive from the given keyset handle.

func NewWithKeyManager

func NewWithKeyManager(h *keyset.Handle, km registry.KeyManager) (tink.MAC, error)

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

PathSynopsis
mac/subtlePackage 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.