# package hmac
`import "crypto/hmac"`
hmac包实现了U.S. Federal Information Processing Standards Publication 198规定的HMAC(加密哈希信息认证码)。
HMAC是使用key标记信息的加密hash。接收者使用相同的key逆运算来认证hash。
出于安全目的,接收者应使用Equal函数比较认证码:
```
// 如果messageMAC是message的合法HMAC标签,函数返回真
func CheckMAC(message, messageMAC, key []byte) bool {
mac := hmac.New(sha256.New, key)
mac.Write(message)
expectedMAC := mac.Sum(nil)
return hmac.Equal(messageMAC, expectedMAC)
}
```
## Index
* [func Equal(mac1, mac2 []byte) bool](#Equal)
* [func New(h func() hash.Hash, key []byte) hash.Hash](#New)
## func [Equal](https://github.com/golang/go/blob/master/src/crypto/hmac/hmac.go#L97 "View Source")
```
func Equal(mac1, mac2 []byte) bool
```
比较两个MAC是否相同,而不会泄露对比时间信息。(以规避时间侧信道攻击:指通过计算比较时花费的时间的长短来获取密码的信息,用于密码破解)
## func [New](https://github.com/golang/go/blob/master/src/crypto/hmac/hmac.go#L78 "View Source")
```
func New(h func() hash.Hash, key []byte) hash.Hash
```
New函数返回一个采用hash.Hash作为底层hash接口、key作为密钥的HMAC算法的hash接口。
- 库
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe