github.com/jcmturner/gokrb5/v8@v8.4.4/crypto/rfc4757/checksum.go (about)

     1  package rfc4757
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/hmac"
     6  	"crypto/md5"
     7  	"io"
     8  )
     9  
    10  // Checksum returns a hash of the data in accordance with RFC 4757
    11  func Checksum(key []byte, usage uint32, data []byte) ([]byte, error) {
    12  	// Create hashing key
    13  	s := append([]byte(`signaturekey`), byte(0x00)) //includes zero octet at end
    14  	mac := hmac.New(md5.New, key)
    15  	mac.Write(s)
    16  	Ksign := mac.Sum(nil)
    17  
    18  	// Format data
    19  	tb := UsageToMSMsgType(usage)
    20  	p := append(tb, data...)
    21  	h := md5.New()
    22  	rb := bytes.NewReader(p)
    23  	_, err := io.Copy(h, rb)
    24  	if err != nil {
    25  		return []byte{}, err
    26  	}
    27  	tmp := h.Sum(nil)
    28  
    29  	// Generate HMAC
    30  	mac = hmac.New(md5.New, Ksign)
    31  	mac.Write(tmp)
    32  	return mac.Sum(nil), nil
    33  }
    34  
    35  // HMAC returns a keyed MD5 checksum of the data
    36  func HMAC(key []byte, data []byte) []byte {
    37  	mac := hmac.New(md5.New, key)
    38  	mac.Write(data)
    39  	return mac.Sum(nil)
    40  }