github.com/maenmax/kairep@v0.0.0-20210218001208-55bf3df36788/src/golang.org/x/crypto/ssh/mac.go (about)

     1  // Copyright 2012 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 ssh
     6  
     7  // Message authentication support
     8  
     9  import (
    10  	"crypto/hmac"
    11  	"crypto/sha1"
    12  	"crypto/sha256"
    13  	"hash"
    14  )
    15  
    16  type macMode struct {
    17  	keySize int
    18  	new     func(key []byte) hash.Hash
    19  }
    20  
    21  // truncatingMAC wraps around a hash.Hash and truncates the output digest to
    22  // a given size.
    23  type truncatingMAC struct {
    24  	length int
    25  	hmac   hash.Hash
    26  }
    27  
    28  func (t truncatingMAC) Write(data []byte) (int, error) {
    29  	return t.hmac.Write(data)
    30  }
    31  
    32  func (t truncatingMAC) Sum(in []byte) []byte {
    33  	out := t.hmac.Sum(in)
    34  	return out[:len(in)+t.length]
    35  }
    36  
    37  func (t truncatingMAC) Reset() {
    38  	t.hmac.Reset()
    39  }
    40  
    41  func (t truncatingMAC) Size() int {
    42  	return t.length
    43  }
    44  
    45  func (t truncatingMAC) BlockSize() int { return t.hmac.BlockSize() }
    46  
    47  var macModes = map[string]*macMode{
    48  	"hmac-sha2-256": {32, func(key []byte) hash.Hash {
    49  		return hmac.New(sha256.New, key)
    50  	}},
    51  	"hmac-sha1": {20, func(key []byte) hash.Hash {
    52  		return hmac.New(sha1.New, key)
    53  	}},
    54  	"hmac-sha1-96": {20, func(key []byte) hash.Hash {
    55  		return truncatingMAC{12, hmac.New(sha1.New, key)}
    56  	}},
    57  }