auth

package
v0.44.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: BSD-3-Clause Imports: 2 Imported by: 25

Documentation

Overview

Package auth authenticates a message using a secret key.

This package is interoperable with NaCl.

The auth package is essentially a wrapper for HMAC-SHA-512 (implemented by crypto/hmac and crypto/sha512), truncated to 32 bytes. It is frozen and is not accepting new features.

Example
package main

import (
	"encoding/hex"
	"fmt"

	"golang.org/x/crypto/nacl/auth"
)

func main() {
	// Load your secret key from a safe place and reuse it across multiple
	// Sum calls. (Obviously don't use this example key for anything
	// real.) If you want to convert a passphrase to a key, use a suitable
	// package like bcrypt or scrypt.
	secretKeyBytes, err := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574")
	if err != nil {
		panic(err)
	}

	var secretKey [32]byte
	copy(secretKey[:], secretKeyBytes)

	mac := auth.Sum([]byte("hello world"), &secretKey)
	fmt.Printf("%x\n", *mac)
	result := auth.Verify(mac[:], []byte("hello world"), &secretKey)
	fmt.Println(result)
	badResult := auth.Verify(mac[:], []byte("different message"), &secretKey)
	fmt.Println(badResult)
}
Output:

eca5a521f3d77b63f567fb0cb6f5f2d200641bc8dada42f60c5f881260c30317
true
false

Index

Examples

Constants

View Source
const (
	// Size is the size, in bytes, of an authenticated digest.
	Size = 32
	// KeySize is the size, in bytes, of an authentication key.
	KeySize = 32
)

Variables

This section is empty.

Functions

func Sum

func Sum(m []byte, key *[KeySize]byte) *[Size]byte

Sum generates an authenticator for m using a secret key and returns the 32-byte digest.

func Verify

func Verify(digest []byte, m []byte, key *[KeySize]byte) bool

Verify checks that digest is a valid authenticator of message m under the given secret key. Verify does not leak timing information.

Types

This section is empty.

Jump to

Keyboard shortcuts

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