// Sum512 computes the 512 bit BLAKE2b checksum (or MAC if a key is specified)
// of msg and saves the sum in out. The key is optional and can be nil.
776524
func Sum512(out *[64]byte, msg, key []byte) {
	var (
		hVal  [8]uint64
		ctr   [2]uint64
		block [blake2b.BlockSize]byte
		off   int
	)
	hVal = hVal512

	if k := len(key); k > 0 {
		hVal[0] ^= uint64(k) << 8 // mixin key-length

		copy(block[:], key)
		off = blake2b.BlockSize
		if len(msg) > 0 {
			blake2b.Core(&hVal, &ctr, blake2b.MsgFlag, block[:])
			off = 0
		}
	}

	if length := len(msg); length > blake2b.BlockSize {
		n := length & (^(blake2b.BlockSize - 1)) // length -= (length % BlockSize)
		if length == n {
			n -= blake2b.BlockSize
		}
		blake2b.Core(&hVal, &ctr, blake2b.MsgFlag, msg[:n])
		msg = msg[n:]
	}

	if len(msg) > 0 {
		off += copy(block[:], msg)
	}

	blake2b.ExtractHash(out, &hVal, &ctr, &block, off)
}
749197
func (sm3 *SM3) Sum(in []byte) []byte {
	sm3.Write(in)
	msg := sm3.pad()

	// Finialize
	sm3.update(msg, len(msg)/sm3.BlockSize())

	// save hash to in
	needed := sm3.Size()
	if cap(in)-len(in) < needed {
		newIn := make([]byte, len(in), len(in)+needed)
		copy(newIn, in)
		in = newIn
	}
	out := in[len(in) : len(in)+needed]

	for i := 0; i < 8; i++ {
		binary.BigEndian.PutUint32(out[i*4:], sm3.digest[i])
	}
	return out

}
724995
func Sum(msg []byte, c cipher.Block) ([]byte, error) {
	mac, err := New(c)
	if err != nil {
		return nil, err
	}
	mac.Write(msg)
	return mac.Sum(nil), nil
}
315042
func (s *State232) Sum(b []byte) []byte {
	s.hash = Hash232(s.tail, s.seed)
	h := s.hash
	return append(b, byte(h>>24), byte(h>>16), byte(h>>8), byte(h))
}
965812
func checksum(ctx context.Context, filename string, h hash.Hash) (b []byte, err error) {
	var f *os.File
	f, err = os.Open(filename)
	if err != nil {
		return
	}
	defer func() {
		err = f.Close()
	}()

	t := newTransfer(ctx, nil, h, f, nil)
	if _, err = t.copy(); err != nil {
		return
	}

	b = h.Sum(nil)
	return
}
314039
func (d *Avx512Digest) Sum(in []byte) (result []byte) {

	if d.final {
		return append(in, d.result[:]...)
	}

	trail := make([]byte, 0, 128)

	len := d.len
	// Padding.  Add a 1 bit and 0 bits until 56 bytes mod 64.
	var tmp [64]byte
	tmp[0] = 0x80
	if len%64 < 56 {
		trail = append(d.x[:d.nx], tmp[0:56-len%64]...)
	} else {
		trail = append(d.x[:d.nx], tmp[0:64+56-len%64]...)
	}
	d.nx = 0

	// Length in bits.
	len <<= 3
	for i := uint(0); i < 8; i++ {
		tmp[i] = byte(len >> (56 - 8*i))
	}
	trail = append(trail, tmp[0:8]...)

	sumCh := make(chan [Size]byte)
	d.a512srv.blocksCh <- blockInput{uid: d.uid, msg: trail, final: true, sumCh: sumCh}
	d.result = <-sumCh
	d.final = true
	return append(in, d.result[:]...)
}
613857
func NewAvx512(a512srv *Avx512Server) hash.Hash {
	uid := atomic.AddUint64(&uidCounter, 1)
	return &Avx512Digest{uid: uid, a512srv: a512srv}
}
910314
func (f *fragment) sum(filter *Row, bitDepth uint) (sum, count uint64, err error) {
	// Compute count based on the existence row.
	consider := f.row(uint64(bitDepth))
	if filter != nil {
		consider = consider.Intersect(filter)
	}
	count = consider.Count()

	// Compute the sum based on the bit count of each row multiplied by the
	// place value of each row. For example, 10 bits in the 1's place plus
	// 4 bits in the 2's place plus 3 bits in the 4's place equals a total
	// sum of 30:
	//
	//   10*(2^0) + 4*(2^1) + 3*(2^2) = 30
	//
	var cnt uint64
	for i := uint(0); i < bitDepth; i++ {
		row := f.row(uint64(i))
		cnt = row.intersectionCount(consider)
		sum += (1 << i) * cnt
	}

	return sum, count, nil
}
413575
func (a512srv *Avx512Server) Sum(uid uint64, p []byte) [32]byte {
	sumCh := make(chan [32]byte)
	a512srv.blocksCh <- blockInput{uid: uid, msg: p, final: true, sumCh: sumCh}
	return <-sumCh
}
374410
func checkMAC(message, messageMAC, key []byte, hashFunc func() hash.Hash) bool {
	expectedMAC := genMAC(message, key, hashFunc)
	return hmac.Equal(messageMAC, expectedMAC)
}
1124657
func Sum512(data []byte) (digest [64]byte) {
	h := New512()
	h.Write(data)
	h.Sum(digest[:0])
	return
}
1051881
func ValidateMd5Checksum(data []byte, sum []byte) bool {
	ourSum := ComputeMd5Checksum(data)
	return bytes.Equal(ourSum, sum)
}
117575
func (f *CryptoFuncs) SHA512_256(input interface{}) string {
	in := toBytes(input)
	out := sha512.Sum512_256(in)
	return fmt.Sprintf("%02x", out)
}
1289979
func Sum384(data []byte) (sum384 [Size384]byte) {
	var d digest
	d.is384 = true
	d.Reset()
	d.Write(data)
	sum := d.checkSum()
	copy(sum384[:], sum[:Size384])
	return
}
942359
func SignRS512Base64(r *rsa.PrivateKey, data []byte) (string, error) {
	sig, err := SignRS512(r, data)
	if err != nil {
		return "", err
	}
	return base64.StdEncoding.EncodeToString(sig), nil
}
32096
func Sum(data []byte, code uint64, length int) (Multihash, error) {
	if !ValidCode(code) {
		return nil, fmt.Errorf("invalid multihash code %d", code)
	}

	if length < 0 {
		var ok bool
		length, ok = DefaultLengths[code]
		if !ok {
			return nil, fmt.Errorf("no default length for code %d", code)
		}
	}

	hashFunc, ok := funcTable[code]
	if !ok {
		return nil, ErrSumNotSupported
	}

	d, err := hashFunc(data, length)
	if err != nil {
		return nil, err
	}
	if length >= 0 {
		d = d[:length]
	}
	return Encode(d, code)
}
363068
func Sha512(data []byte) string {
	sha512 := sha512.New()
	sha512.Write(data)
	return hex.EncodeToString(sha512.Sum(nil))
}