crypto: Index | Examples | Files

package auth

import ""

Package auth authenticates a message using a secret key.

The Sum function, viewed as a function of the message for a uniform random key, is designed to meet the standard notion of unforgeability. This means that an attacker cannot find authenticators for any messages not authenticated by the sender, even if the attacker has adaptively influenced the messages authenticated by the sender. For a formal definition see, e.g., Section 2.4 of Bellare, Kilian, and Rogaway, "The security of the cipher block chaining message authentication code," Journal of Computer and System Sciences 61 (2000), 362–399;

auth does not make any promises regarding "strong" unforgeability; perhaps one valid authenticator can be converted into another valid authenticator for the same message. NaCl also does not make any promises regarding "truncated unforgeability."

This package is interoperable with NaCl:


// 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 {

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)
badResult := auth.Verify(mac[:], []byte("different message"), &secretKey)





Package Files



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

func Sum Uses

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 Uses

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.

Package auth imports 2 packages (graph) and is imported by 12 packages. Updated 2020-12-22. Refresh now. Tools for package owners.