mac

package
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2022 License: Apache-2.0 Imports: 14 Imported by: 20

Documentation

Overview

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
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))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

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

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

New creates a MAC primitive from the given keyset handle.

func NewWithKeyManager deprecated

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.

Types

This section is empty.

Directories

Path Synopsis
Package subtle provides subtle implementations of the MAC primitive.
Package subtle provides subtle implementations of the MAC primitive.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL