// CalculateChecksum calculates the checksum of the udp packet, given the total
// length of the packet and the checksum of the network-layer pseudo-header
// (excluding the total length) and the checksum of the payload.
165142
func (b UDP) CalculateChecksum(partialChecksum uint16, totalLen uint16) uint16 {
	// Add the length portion of the checksum to the pseudo-checksum.
	tmp := make([]byte, 2)
	binary.BigEndian.PutUint16(tmp, totalLen)
	checksum := Checksum(tmp, partialChecksum)

	// Calculate the rest of the checksum.
	return Checksum(b[:UDPMinimumSize], checksum)
}
1273211
func (dc *CDA) Checksum() (Trytes, error) {
	// checksum formula:
	// Checksum = CurlHash(
	// 	CurlHash(address_trits)[:134] +
	// 	PadTrits27(timeout_value_trits) +
	// 	MultiUse(0/1) +
	// 	PadTrits81(amount_value_trits)
	// )
	if err := ValidateConditions(&dc.Conditions); err != nil {
		return "", err
	}
	addrTrits, err := TrytesToTrits(dc.Address[:consts.HashTrytesSize])
	if err != nil {
		return "", err
	}
	c := curl.NewCurlP81()
	if err := c.Absorb(addrTrits); err != nil {
		return "", err
	}
	addrChecksumTrits, err := c.Squeeze(consts.HashTrinarySize)
	if err != nil {
		return "", err
	}
	timeoutAtTrits := PadTrits(IntToTrits(dc.TimeoutAt.Unix()), 27)
	var expectedAmountTrits Trits
	if dc.ExpectedAmount != nil {
		expectedAmountTrits = PadTrits(IntToTrits(int64(*dc.ExpectedAmount)), 81)
	} else {
		expectedAmountTrits = PadTrits(expectedAmountTrits, 81)
	}
	var multiUse int8
	if dc.MultiUse {
		multiUse = 1
	}
	input := make(Trits, 0)
	input = append(input, addrChecksumTrits[:134]...)
	input = append(input, timeoutAtTrits...)
	input = append(input, multiUse)
	input = append(input, expectedAmountTrits...)
	c.Reset()
	if err := c.Absorb(input); err != nil {
		return "", err
	}
	checksumTrits, err := c.Squeeze(consts.HashTrinarySize)
	if err != nil {
		return "", err
	}
	return TritsToTrytes(checksumTrits)
}
640314
func getChecksum(data interface{}) uint64 {
	hash := fnv.New32a()
	hashutil.DeepHashObject(hash, data)
	return uint64(hash.Sum32())
}
3166
func (s *ArchiveCreationOutput) SetChecksum(v string) *ArchiveCreationOutput {
	s.Checksum = &v
	return s
}
20227
func (s *CompleteMultipartUploadInput) SetChecksum(v string) *CompleteMultipartUploadInput {
	s.Checksum = &v
	return s
}
1143519
func (b UDP) SetChecksum(checksum uint16) {
	binary.BigEndian.PutUint16(b[udpChecksum:], checksum)
}
964843
func (b ICMPv4) SetChecksum(checksum uint16) {
	binary.BigEndian.PutUint16(b[2:], checksum)
}
496008
func (s *PutIntentOutput) SetChecksum(v string) *PutIntentOutput {
	s.Checksum = &v
	return s
}
826433
func (s *GetJobOutputOutput) SetChecksum(v string) *GetJobOutputOutput {
	s.Checksum = &v
	return s
}
1149684
func verifyChecksum(path, expectedSum string) (bool, error) {
	md5sum, err := checksum(path)
	if err != nil {
		return false, err
	}

	log.Debug("expected MD5 sum:", expectedSum)
	log.Debug("MD5 sum found:", md5sum)

	return expectedSum == md5sum, nil
}
1033650
func (rdb RethinkDB) GetChecksum(gun data.GUN, role data.RoleName, checksum string) (created *time.Time, data []byte, err error) {
	var file RDBTUFFile
	res, err := gorethink.DB(rdb.dbName).Table(file.TableName(), gorethink.TableOpts{ReadMode: "majority"}).GetAllByIndex(
		rdbGunRoleSHA256Idx, []string{gun.String(), role.String(), checksum},
	).Run(rdb.sess)
	if err != nil {
		return nil, nil, err
	}
	defer res.Close()
	if res.IsNil() {
		return nil, nil, ErrNotFound{}
	}
	err = res.One(&file)
	if err == gorethink.ErrEmptyResult {
		return nil, nil, ErrNotFound{}
	}
	return &file.CreatedAt, file.Data, err
}
317658
func (f *fragment) Checksum() []byte {
	h := xxhash.New()
	for _, block := range f.Blocks() {
		_, _ = h.Write(block.Checksum)
	}
	return h.Sum(nil)
}
660813
func (e *RawV4Event) Checksum() []byte {
	return e.data[len(e.data)-e.checksumSize:]
}
250636
func GenerateChecksum(data []byte) byte {
	var offset int
	ret := data[offset]
	for i := offset + 1; i < len(data)-1; i++ {
		// Xor bytes
		ret ^= data[i]
	}
	// Not result
	ret = (byte)(^ret)
	return ret
}
1000234
func (p *Packet) UpdateIPChecksum() {

	p.ipHdr.ipChecksum = p.computeIPChecksum()

	binary.BigEndian.PutUint16(p.ipHdr.Buffer[ipv4ChecksumPos:ipv4ChecksumPos+2], p.ipHdr.ipChecksum)
}
127959
func addChecksum(data []byte) []byte {
	// Get first byte of sha256
	hash := computeChecksum(data)
	firstChecksumByte := hash[0]

	// len() is in bytes so we divide by 4
	checksumBitLength := uint(len(data) / 4)

	// For each bit of check sum we want we shift the data one the left
	// and then set the (new) right most bit equal to checksum bit at that index
	// staring from the left
	dataBigInt := new(big.Int).SetBytes(data)
	for i := uint(0); i < checksumBitLength; i++ {
		// Bitshift 1 left
		dataBigInt.Mul(dataBigInt, bigTwo)

		// Set rightmost bit if leftmost checksum bit is set
		if uint8(firstChecksumByte&(1<<(7-i))) > 0 {
			dataBigInt.Or(dataBigInt, bigOne)
		}
	}

	return dataBigInt.Bytes()
}
96707
func (st *MemStorage) GetChecksum(gun data.GUN, role data.RoleName, checksum string) (*time.Time, []byte, error) {
	st.lock.Lock()
	defer st.lock.Unlock()
	space, ok := st.checksums[gun.String()][checksum]
	if !ok || len(space.data) == 0 {
		return nil, nil, ErrNotFound{}
	}
	return &(space.createupdate), space.data, nil
}