package digest

import (
	
	
	
	
	
	
	
	
	
	
)

// Prefix for digest authentication headers
const Prefix = "Digest "

// IsDigest returns true if the header value is a digest auth header
func ( string) bool {
	if len() < len(Prefix) {
		return false
	}
	return strings.EqualFold([:len(Prefix)], Prefix)
}

// CutPrefix removes the digest prefix from the header value
func ( string) (string, bool) {
	if !IsDigest() {
		return , false
	}
	return [len(Prefix):], true
}

// Options for creating a credentials
type Options struct {
	Method   string
	URI      string
	GetBody  func() (io.ReadCloser, error)
	Count    int
	Username string
	Password string

	// The following are provided for advanced use cases where the client needs
	// to override the default digest calculation behavior. Most users should
	// leave these fields unset.
	A1     string
	Cnonce string
}

// CanDigest checks if the algorithm and qop are supported
func ( *Challenge) bool {
	switch strings.ToUpper(.Algorithm) {
	case "", "MD5", "SHA-256", "SHA-512", "SHA-512-256":
	default:
		return false
	}
	return len(.QOP) == 0 || .SupportsQOP("auth") || .SupportsQOP("auth-int")
}

// Digest creates credentials from a challenge and request options.
// Note: if you want to re-use a challenge, you must increment the Count.
func ( *Challenge,  Options) (*Credentials, error) {
	 := &Credentials{
		Username:  .Username,
		URI:       .URI,
		Cnonce:    .Cnonce,
		Nc:        .Count,
		Realm:     .Realm,
		Nonce:     .Nonce,
		Algorithm: .Algorithm,
		Opaque:    .Opaque,
		Userhash:  .Userhash,
	}
	// we re-use the same hash.Hash
	var  hash.Hash
	switch strings.ToUpper(.Algorithm) {
	case "", "MD5":
		 = md5.New()
	case "SHA-256":
		 = sha256.New()
	case "SHA-512":
		 = sha512.New()
	case "SHA-512-256":
		 = sha512.New512_256()
	default:
		return nil, fmt.Errorf("digest: unsupported algorithm: %q", .Algorithm)
	}
	// hash the username if requested
	if .Userhash {
		.Username = hashjoin(, .Username, .Realm)
	}
	// generate the a1 hash if one was not provided
	 := .A1
	if  == "" {
		 = hashjoin(, .Username, .Realm, .Password)
	}
	// generate the response
	switch {
	case len(.QOP) == 0:
		.Response = hashjoin(,
			,
			.Nonce,
			hashjoin(, .Method, .URI), // A2
		)
	case .SupportsQOP("auth"):
		.QOP = "auth"
		if .Cnonce == "" {
			.Cnonce = cnonce()
		}
		if .Nc == 0 {
			.Nc = 1
		}
		.Response = hashjoin(,
			,
			.Nonce,
			fmt.Sprintf("%08x", .Nc),
			.Cnonce,
			.QOP,
			hashjoin(, .Method, .URI), // A2
		)
	case .SupportsQOP("auth-int"):
		.QOP = "auth-int"
		if .Cnonce == "" {
			.Cnonce = cnonce()
		}
		if .Nc == 0 {
			.Nc = 1
		}
		,  := hashbody(, .GetBody)
		if  != nil {
			return nil, fmt.Errorf("digest: failed to read body for auth-int: %w", )
		}
		.Response = hashjoin(,
			,
			.Nonce,
			fmt.Sprintf("%08x", .Nc),
			.Cnonce,
			.QOP,
			hashjoin(, .Method, .URI, ), // A2
		)
	default:
		return nil, fmt.Errorf("digest: unsupported qop: %q", strings.Join(.QOP, ","))
	}
	return , nil
}

func hashjoin( hash.Hash,  ...string) string {
	.Reset()
	for ,  := range  {
		if  > 0 {
			.Write([]byte(":"))
		}
		.Write([]byte())
	}
	return hex.EncodeToString(.Sum(nil))
}

func hashbody( hash.Hash,  func() (io.ReadCloser, error)) (string, error) {
	.Reset()
	if  != nil {
		,  := ()
		if  != nil {
			return "", 
		}
		defer .Close()
		if  != http.NoBody {
			if ,  := io.Copy(, );  != nil {
				return "", 
			}
		}
	}
	return hex.EncodeToString(.Sum(nil)), nil
}

func cnonce() string {
	var  [8]byte
	if ,  := rand.Read([:]);  != nil {
		panic(fmt.Sprintf("digest: failed to generate cnonce: %v", ))
	}
	return hex.EncodeToString([:])
}