The Go Programming Language

Source file src/pkg/crypto/hmac/hmac.go

     1	// Copyright 2009 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// Package hmac implements the Keyed-Hash Message Authentication Code (HMAC) as
     6	// defined in U.S. Federal Information Processing Standards Publication 198.
     7	// An HMAC is a cryptographic hash that uses a key to sign a message.
     8	// The receiver verifies the hash by recomputing it using the same key.
     9	package hmac
    10	
    11	import (
    12		"crypto/md5"
    13		"crypto/sha1"
    14		"crypto/sha256"
    15		"hash"
    16		"os"
    17	)
    18	
    19	// FIPS 198:
    20	// http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf
    21	
    22	// key is zero padded to 64 bytes
    23	// ipad = 0x36 byte repeated to 64 bytes
    24	// opad = 0x5c byte repeated to 64 bytes
    25	// hmac = H([key ^ opad] H([key ^ ipad] text))
    26	
    27	const (
    28		// NOTE(rsc): This constant is actually the
    29		// underlying hash function's block size.
    30		// HMAC is only conventionally used with
    31		// MD5 and SHA1, and both use 64-byte blocks.
    32		// The hash.Hash interface doesn't provide a
    33		// way to find out the block size.
    34		padSize = 64
    35	)
    36	
    37	type hmac struct {
    38		size         int
    39		key, tmp     []byte
    40		outer, inner hash.Hash
    41	}
    42	
    43	func (h *hmac) tmpPad(xor byte) {
    44		for i, k := range h.key {
    45			h.tmp[i] = xor ^ k
    46		}
    47		for i := len(h.key); i < padSize; i++ {
    48			h.tmp[i] = xor
    49		}
    50	}
    51	
    52	func (h *hmac) Sum() []byte {
    53		sum := h.inner.Sum()
    54		h.tmpPad(0x5c)
    55		for i, b := range sum {
    56			h.tmp[padSize+i] = b
    57		}
    58		h.outer.Reset()
    59		h.outer.Write(h.tmp)
    60		return h.outer.Sum()
    61	}
    62	
    63	func (h *hmac) Write(p []byte) (n int, err os.Error) {
    64		return h.inner.Write(p)
    65	}
    66	
    67	func (h *hmac) Size() int { return h.size }
    68	
    69	func (h *hmac) Reset() {
    70		h.inner.Reset()
    71		h.tmpPad(0x36)
    72		h.inner.Write(h.tmp[0:padSize])
    73	}
    74	
    75	// New returns a new HMAC hash using the given hash generator and key.
    76	func New(h func() hash.Hash, key []byte) hash.Hash {
    77		hm := new(hmac)
    78		hm.outer = h()
    79		hm.inner = h()
    80		hm.size = hm.inner.Size()
    81		hm.tmp = make([]byte, padSize+hm.size)
    82		if len(key) > padSize {
    83			// If key is too big, hash it.
    84			hm.outer.Write(key)
    85			key = hm.outer.Sum()
    86		}
    87		hm.key = make([]byte, len(key))
    88		copy(hm.key, key)
    89		hm.Reset()
    90		return hm
    91	}
    92	
    93	// NewMD5 returns a new HMAC-MD5 hash using the given key.
    94	func NewMD5(key []byte) hash.Hash { return New(md5.New, key) }
    95	
    96	// NewSHA1 returns a new HMAC-SHA1 hash using the given key.
    97	func NewSHA1(key []byte) hash.Hash { return New(sha1.New, key) }
    98	
    99	// NewSHA256 returns a new HMAC-SHA256 hash using the given key.
   100	func NewSHA256(key []byte) hash.Hash { return New(sha256.New, key) }

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.