// Digest computes the digest derived from the signature fragment and bundle hash.
// Optionally takes the SpongeFunction to use. Default is Kerl.
345436
func Digest(bundleHash []int8, signatureFragment Trits, startOffset int, spongeFunc ...SpongeFunction) (Trits, error) {
	sigLength := len(signatureFragment)

	secLvl, err := GetSecurityLevel(bundleHash)
	if err != nil {
		return nil, err
	}

	sigFrag := make(Trits, sigLength)
	copy(sigFrag, signatureFragment[:sigLength])

	h := GetSpongeFunc(spongeFunc, kerl.NewKerl)
	defer h.Reset()

	dig := make(Trits, HashTrinarySize)
	sig := make(Trits, HashTrinarySize)

	i := startOffset % int(secLvl)
	for digLength := 0; digLength < sigLength; {
		for j := i * ISSChunkLength; j < (i+1)*ISSChunkLength && digLength < sigLength; j += TrinaryRadix {
			copy(sig, sigFrag[digLength:digLength+HashTrinarySize])

			to := (bundleHash[j] + bundleHash[j+1]*3 + bundleHash[j+2]*9) - MinTryteValue

			for k := 0; k < int(to); k++ {
				err := h.Absorb(sig)
				if err != nil {
					return nil, err
				}
				sig, err = h.Squeeze(HashTrinarySize)
				if err != nil {
					return nil, err
				}
				h.Reset()
			}

			copy(sigFrag[digLength:], sig)
			digLength += HashTrinarySize
		}

		i = (i + 1) % int(secLvl)
	}

	err = h.Absorb(sigFrag)
	if err != nil {
		return nil, err
	}

	dig, err = h.Squeeze(HashTrinarySize)
	if err != nil {
		return nil, err
	}

	return dig, nil
}
839694
func (d *digest) keccakF() {
	for _, roundConstant := range rc {
		// θ step
		d.c[0] = d.a[0] ^ d.a[5] ^ d.a[10] ^ d.a[15] ^ d.a[20]
		d.c[1] = d.a[1] ^ d.a[6] ^ d.a[11] ^ d.a[16] ^ d.a[21]
		d.c[2] = d.a[2] ^ d.a[7] ^ d.a[12] ^ d.a[17] ^ d.a[22]
		d.c[3] = d.a[3] ^ d.a[8] ^ d.a[13] ^ d.a[18] ^ d.a[23]
		d.c[4] = d.a[4] ^ d.a[9] ^ d.a[14] ^ d.a[19] ^ d.a[24]

		d.d[0] = d.c[4] ^ (d.c[1]<<1 ^ d.c[1]>>63)
		d.d[1] = d.c[0] ^ (d.c[2]<<1 ^ d.c[2]>>63)
		d.d[2] = d.c[1] ^ (d.c[3]<<1 ^ d.c[3]>>63)
		d.d[3] = d.c[2] ^ (d.c[4]<<1 ^ d.c[4]>>63)
		d.d[4] = d.c[3] ^ (d.c[0]<<1 ^ d.c[0]>>63)

		d.a[0] ^= d.d[0]
		d.a[1] ^= d.d[1]
		d.a[2] ^= d.d[2]
		d.a[3] ^= d.d[3]
		d.a[4] ^= d.d[4]
		d.a[5] ^= d.d[0]
		d.a[6] ^= d.d[1]
		d.a[7] ^= d.d[2]
		d.a[8] ^= d.d[3]
		d.a[9] ^= d.d[4]
		d.a[10] ^= d.d[0]
		d.a[11] ^= d.d[1]
		d.a[12] ^= d.d[2]
		d.a[13] ^= d.d[3]
		d.a[14] ^= d.d[4]
		d.a[15] ^= d.d[0]
		d.a[16] ^= d.d[1]
		d.a[17] ^= d.d[2]
		d.a[18] ^= d.d[3]
		d.a[19] ^= d.d[4]
		d.a[20] ^= d.d[0]
		d.a[21] ^= d.d[1]
		d.a[22] ^= d.d[2]
		d.a[23] ^= d.d[3]
		d.a[24] ^= d.d[4]

		// ρ and π steps
		d.b[0] = d.a[0]
		d.b[1] = d.a[6]<<ro_06 ^ d.a[6]>>(64-ro_06)
		d.b[2] = d.a[12]<<ro_12 ^ d.a[12]>>(64-ro_12)
		d.b[3] = d.a[18]<<ro_18 ^ d.a[18]>>(64-ro_18)
		d.b[4] = d.a[24]<<ro_24 ^ d.a[24]>>(64-ro_24)
		d.b[5] = d.a[3]<<ro_15 ^ d.a[3]>>(64-ro_15)
		d.b[6] = d.a[9]<<ro_21 ^ d.a[9]>>(64-ro_21)
		d.b[7] = d.a[10]<<ro_02 ^ d.a[10]>>(64-ro_02)
		d.b[8] = d.a[16]<<ro_08 ^ d.a[16]>>(64-ro_08)
		d.b[9] = d.a[22]<<ro_14 ^ d.a[22]>>(64-ro_14)
		d.b[10] = d.a[1]<<ro_05 ^ d.a[1]>>(64-ro_05)
		d.b[11] = d.a[7]<<ro_11 ^ d.a[7]>>(64-ro_11)
		d.b[12] = d.a[13]<<ro_17 ^ d.a[13]>>(64-ro_17)
		d.b[13] = d.a[19]<<ro_23 ^ d.a[19]>>(64-ro_23)
		d.b[14] = d.a[20]<<ro_04 ^ d.a[20]>>(64-ro_04)
		d.b[15] = d.a[4]<<ro_20 ^ d.a[4]>>(64-ro_20)
		d.b[16] = d.a[5]<<ro_01 ^ d.a[5]>>(64-ro_01)
		d.b[17] = d.a[11]<<ro_07 ^ d.a[11]>>(64-ro_07)
		d.b[18] = d.a[17]<<ro_13 ^ d.a[17]>>(64-ro_13)
		d.b[19] = d.a[23]<<ro_19 ^ d.a[23]>>(64-ro_19)
		d.b[20] = d.a[2]<<ro_10 ^ d.a[2]>>(64-ro_10)
		d.b[21] = d.a[8]<<ro_16 ^ d.a[8]>>(64-ro_16)
		d.b[22] = d.a[14]<<ro_22 ^ d.a[14]>>(64-ro_22)
		d.b[23] = d.a[15]<<ro_03 ^ d.a[15]>>(64-ro_03)
		d.b[24] = d.a[21]<<ro_09 ^ d.a[21]>>(64-ro_09)

		// χ step
		d.a[0] = d.b[0] ^ (^d.b[1] & d.b[2])
		d.a[1] = d.b[1] ^ (^d.b[2] & d.b[3])
		d.a[2] = d.b[2] ^ (^d.b[3] & d.b[4])
		d.a[3] = d.b[3] ^ (^d.b[4] & d.b[0])
		d.a[4] = d.b[4] ^ (^d.b[0] & d.b[1])
		d.a[5] = d.b[5] ^ (^d.b[6] & d.b[7])
		d.a[6] = d.b[6] ^ (^d.b[7] & d.b[8])
		d.a[7] = d.b[7] ^ (^d.b[8] & d.b[9])
		d.a[8] = d.b[8] ^ (^d.b[9] & d.b[5])
		d.a[9] = d.b[9] ^ (^d.b[5] & d.b[6])
		d.a[10] = d.b[10] ^ (^d.b[11] & d.b[12])
		d.a[11] = d.b[11] ^ (^d.b[12] & d.b[13])
		d.a[12] = d.b[12] ^ (^d.b[13] & d.b[14])
		d.a[13] = d.b[13] ^ (^d.b[14] & d.b[10])
		d.a[14] = d.b[14] ^ (^d.b[10] & d.b[11])
		d.a[15] = d.b[15] ^ (^d.b[16] & d.b[17])
		d.a[16] = d.b[16] ^ (^d.b[17] & d.b[18])
		d.a[17] = d.b[17] ^ (^d.b[18] & d.b[19])
		d.a[18] = d.b[18] ^ (^d.b[19] & d.b[15])
		d.a[19] = d.b[19] ^ (^d.b[15] & d.b[16])
		d.a[20] = d.b[20] ^ (^d.b[21] & d.b[22])
		d.a[21] = d.b[21] ^ (^d.b[22] & d.b[23])
		d.a[22] = d.b[22] ^ (^d.b[23] & d.b[24])
		d.a[23] = d.b[23] ^ (^d.b[24] & d.b[20])
		d.a[24] = d.b[24] ^ (^d.b[20] & d.b[21])

		// ι step
		d.a[0] ^= roundConstant
	}
}
94658
func ContentDigestForManifest(manifest distribution.Manifest, algo digest.Algorithm) (digest.Digest, error) {
	switch t := manifest.(type) {
	case *schema1.SignedManifest:
		// schema1 manifest digests are calculated from the payload
		if len(t.Canonical) == 0 {
			return "", fmt.Errorf("the schema1 manifest does not have a canonical representation")
		}
		return algo.FromBytes(t.Canonical), nil
	default:
		_, payload, err := manifest.Payload()
		if err != nil {
			return "", err
		}
		return algo.FromBytes(payload), nil
	}
}
255822
func newDigest(size int, key []byte) *digest {
	if size != Size && size != Size128 {
		panic("size must be 8 or 16")
	}
	d := new(digest)
	d.k0 = uint64(key[0]) | uint64(key[1])<<8 | uint64(key[2])<<16 | uint64(key[3])<<24 |
		uint64(key[4])<<32 | uint64(key[5])<<40 | uint64(key[6])<<48 | uint64(key[7])<<56
	d.k1 = uint64(key[8]) | uint64(key[9])<<8 | uint64(key[10])<<16 | uint64(key[11])<<24 |
		uint64(key[12])<<32 | uint64(key[13])<<40 | uint64(key[14])<<48 | uint64(key[15])<<56
	d.size = size
	d.Reset()
	return d
}
152151
func Parse(s string) (Spec, error) {
	u, err := url.Parse("dummy://" + s)
	if err != nil {
		return Spec{}, err
	}

	if u.Scheme != "dummy" {
		return Spec{}, ErrInvalid
	}

	if u.Host == "" {
		return Spec{}, ErrHostnameRequired
	}

	var object string

	if idx := splitRe.FindStringIndex(u.Path); idx != nil {
		// This allows us to retain the @ to signify digests or shortened digests in
		// the object.
		object = u.Path[idx[0]:]
		if object[:1] == ":" {
			object = object[1:]
		}
		u.Path = u.Path[:idx[0]]
	}

	return Spec{
		Locator: path.Join(u.Host, u.Path),
		Object:  object,
	}, nil
}
174975
func (d *digest) Sum(in []byte) []byte {
	// Make a copy of the original hash so that caller can keep writing and summing.
	dup := *d
	dup.finalize()
	return dup.squeeze(in, dup.outputSize)
}
722588
func DigestTranslator(prefix string) func(digest.Digest) string {
	return func(dgst digest.Digest) string {
		return prefix + "@" + dgst.String()
	}
}
727919
func (s *MerkleStoreImpl) fetch(m libkb.MetaContext, hash keybase1.MerkleStoreKitHash) (keybase1.MerkleStoreKit, error) {
	m.Debug("MerkleStore: fetching from server: %s", hash)
	var res merkleStoreServerRes
	err := m.G().API.GetDecode(m, libkb.APIArg{
		Endpoint:    s.endpoint,
		SessionType: libkb.APISessionTypeNONE,
		Args: libkb.HTTPArgs{
			"hash": libkb.S{Val: string(hash)},
		},
	}, &res)
	if err != nil {
		return "", NewMerkleStoreError(err.Error())
	}
	if res.KitJSON == "" {
		return "", NewMerkleStoreError("server returned empty kit for %s", s.tag)
	}
	if s.hash(res.KitJSON) != hash {
		m.Debug("%s hash mismatch: got:%s expected:%s", s.tag, s.hash(res.KitJSON), hash)
		return "", NewMerkleStoreError("server returned wrong kit for %s", s.tag)
	}
	return res.KitJSON, nil
}
620779
func (h *HTTPBin) DigestAuth(w http.ResponseWriter, r *http.Request) {
	parts := strings.Split(r.URL.Path, "/")
	count := len(parts)

	if count != 5 && count != 6 {
		http.Error(w, "Not Found", http.StatusNotFound)
		return
	}

	qop := strings.ToLower(parts[2])
	user := parts[3]
	password := parts[4]

	algoName := "MD5"
	if count == 6 {
		algoName = strings.ToUpper(parts[5])
	}

	if qop != "auth" {
		http.Error(w, "Invalid QOP directive", http.StatusBadRequest)
		return
	}
	if algoName != "MD5" && algoName != "SHA-256" {
		http.Error(w, "Invalid algorithm", http.StatusBadRequest)
		return
	}

	algorithm := digest.MD5
	if algoName == "SHA-256" {
		algorithm = digest.SHA256
	}

	if !digest.Check(r, user, password) {
		w.Header().Set("WWW-Authenticate", digest.Challenge("go-httpbin", algorithm))
		w.WriteHeader(http.StatusUnauthorized)
		return
	}

	resp, _ := json.Marshal(&authResponse{
		Authorized: true,
		User:       user,
	})
	writeJSON(w, resp, http.StatusOK)
}
823395
func H(data string) string {
	digest := md5.New()
	digest.Write([]byte(data))
	return fmt.Sprintf("%x", digest.Sum(nil))
}
1262991
func (c *Ctx) DigestUpdate(sh SessionHandle, message []byte) error {
	e := C.DigestUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(message), C.CK_ULONG(len(message)))
	if toError(e) != nil {
		return toError(e)
	}
	return nil
}
689675
func SignatureFragment(bundleHash Trits, keyFragment Trits, startOffset int, spongeFunc ...SpongeFunction) (Trits, error) {

	secLvl, err := GetSecurityLevel(bundleHash)
	if err != nil {
		return nil, err
	}

	keyLength := int(secLvl) * int(ISSKeyLength)
	sigFrag := make(Trits, keyLength)

	h := GetSpongeFunc(spongeFunc, kerl.NewKerl)
	defer h.Reset()

	sig := make(Trits, HashTrinarySize)

	i := startOffset % int(secLvl)
	for sigLength := 0; sigLength < keyLength; {
		for j := i * ISSChunkLength; j < (i+1)*ISSChunkLength && sigLength < keyLength; j += TrinaryRadix {
			copy(sig, keyFragment[sigLength:sigLength+HashTrinarySize])

			to := MaxTryteValue - (bundleHash[j] + bundleHash[j+1]*3 + bundleHash[j+2]*9)

			for k := 0; k < int(to); k++ {
				err := h.Absorb(sig)
				if err != nil {
					return nil, err
				}
				sig, err = h.Squeeze(HashTrinarySize)
				if err != nil {
					return nil, err
				}
				h.Reset()
			}

			copy(sigFrag[sigLength:], sig)
			sigLength += HashTrinarySize
		}

		i = (i + 1) % int(secLvl)
	}

	return sigFrag, nil
}
205888
func blockAvx512(digests *[512]byte, input [16][]byte, mask []uint64) [16][Size]byte {

	scratch := [512]byte{}
	sha256X16Avx512(digests, &scratch, &table, mask, input)

	output := [16][Size]byte{}
	for i := 0; i < 16; i++ {
		output[i] = getDigest(i, digests[:])
	}

	return output
}
1221562
func (f *FeatureFlagGate) DigestError(m MetaContext, err error) {
	if err == nil {
		return
	}
	ffe, ok := err.(FeatureFlagError)
	if !ok {
		return
	}
	if ffe.Feature() != f.feature {
		m.Debug("Got feature flag error for wrong feature: %v", err)
		return
	}

	m.Debug("Server reports feature %q is flagged off", f.feature)

	f.Lock()
	defer f.Unlock()
	f.lastCheck = m.G().Clock().Now()
	f.lastError = err
}
636961
func (s *fs) Get(dgst digest.Digest) ([]byte, error) {
	s.RLock()
	defer s.RUnlock()

	return s.get(dgst)
}
509597
func digestFromPath(digestPath string) (digest.Digest, error) {

	digestPath = strings.TrimSuffix(digestPath, "/data")
	dir, hex := path.Split(digestPath)
	dir = path.Dir(dir)
	dir, next := path.Split(dir)

	// next is either the algorithm OR the first two characters in the hex string
	var algo string
	if next == hex[:2] {
		algo = path.Base(dir)
	} else {
		algo = next
	}

	dgst := digest.NewDigestFromHex(algo, hex)
	return dgst, dgst.Validate()
}
826142
func SignatureFragment(normalizedBundleHashFragment Trits, keyFragment Trits, spongeFunc ...SpongeFunction) (Trits, error) {
	sigFrag := make(Trits, len(keyFragment))
	copy(sigFrag, keyFragment)

	h := GetSpongeFunc(spongeFunc, kerl.NewKerl)
	defer h.Reset()

	for i := 0; i < KeySegmentsPerFragment; i++ {
		hash := sigFrag[i*HashTrinarySize : (i+1)*HashTrinarySize]

		to := MaxTryteValue - normalizedBundleHashFragment[i]
		for j := 0; j < int(to); j++ {
			if err := h.Absorb(hash); err != nil {
				return nil, err
			}
			var err error
			hash, err = h.Squeeze(HashTrinarySize)
			if err != nil {
				return nil, err
			}
			h.Reset()
		}

		copy(sigFrag[i*HashTrinarySize:], hash)
	}

	return sigFrag, nil
}