diff options
| author | 2025-10-14 15:34:01 +0200 | |
|---|---|---|
| committer | 2025-11-17 14:10:21 +0100 | |
| commit | a041f9f31d7341618d4e139be701b1e798fe94c7 (patch) | |
| tree | b2ba5cddb4c1ba9e3b8bc92ed504167a19983335 /vendor/code.superseriousbusiness.org/httpsig/digest.go | |
| parent | [performance] handle emoji refreshes asynchronously when fetched as part of a... (diff) | |
| download | gotosocial-a041f9f31d7341618d4e139be701b1e798fe94c7.tar.xz | |
[performance] pull in latest httpsig with performance enhancements and bugfixes (#4500)
code.supseriousbusiness.org/httpsig: v1.4.0 -> v1.5.0
Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4500
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/code.superseriousbusiness.org/httpsig/digest.go')
| -rw-r--r-- | vendor/code.superseriousbusiness.org/httpsig/digest.go | 52 |
1 files changed, 27 insertions, 25 deletions
diff --git a/vendor/code.superseriousbusiness.org/httpsig/digest.go b/vendor/code.superseriousbusiness.org/httpsig/digest.go index bf9e3a914..140b10ac5 100644 --- a/vendor/code.superseriousbusiness.org/httpsig/digest.go +++ b/vendor/code.superseriousbusiness.org/httpsig/digest.go @@ -14,32 +14,38 @@ type DigestAlgorithm string const ( DigestSha256 DigestAlgorithm = "SHA-256" - DigestSha512 = "SHA-512" + DigestSha512 DigestAlgorithm = "SHA-512" ) -var digestToDef = map[DigestAlgorithm]crypto.Hash{ - DigestSha256: crypto.SHA256, - DigestSha512: crypto.SHA512, +// hashForDigest returns a hash algorithm for digest algorithm string. +func hashForDigest(algo DigestAlgorithm) crypto.Hash { + switch algo { + case DigestSha256: + return crypto.SHA256 + case DigestSha512: + return crypto.SHA512 + default: + return 0 + } } // IsSupportedDigestAlgorithm returns true if hte string is supported by this // library, is not a hash known to be weak, and is supported by the hardware. func IsSupportedDigestAlgorithm(algo string) bool { uc := DigestAlgorithm(strings.ToUpper(algo)) - c, ok := digestToDef[uc] - return ok && c.Available() + return hashForDigest(uc).Available() } -func getHash(alg DigestAlgorithm) (h hash.Hash, toUse DigestAlgorithm, err error) { - upper := DigestAlgorithm(strings.ToUpper(string(alg))) - c, ok := digestToDef[upper] - if !ok { - err = fmt.Errorf("unknown or unsupported Digest algorithm: %s", alg) +func getHash(algo DigestAlgorithm) (h hash.Hash, toUse DigestAlgorithm, err error) { + uc := DigestAlgorithm(strings.ToUpper(string(algo))) + c := hashForDigest(uc) + if c == 0 { + err = fmt.Errorf("unknown or unsupported Digest algorithm: %s", algo) } else if !c.Available() { - err = fmt.Errorf("unavailable Digest algorithm: %s", alg) + err = fmt.Errorf("unavailable Digest algorithm: %s", algo) } else { h = c.New() - toUse = upper + toUse = uc } return } @@ -56,18 +62,16 @@ func addDigest(r *http.Request, algo DigestAlgorithm, b []byte) (err error) { return } var h hash.Hash - var a DigestAlgorithm - h, a, err = getHash(algo) + h, algo, err = getHash(algo) if err != nil { return } h.Write(b) sum := h.Sum(nil) r.Header.Add(digestHeader, - fmt.Sprintf("%s%s%s", - a, - digestDelim, - base64.StdEncoding.EncodeToString(sum[:]))) + string(algo)+ + digestDelim+ + base64.StdEncoding.EncodeToString(sum[:])) return } @@ -78,18 +82,16 @@ func addDigestResponse(r http.ResponseWriter, algo DigestAlgorithm, b []byte) (e return } var h hash.Hash - var a DigestAlgorithm - h, a, err = getHash(algo) + h, algo, err = getHash(algo) if err != nil { return } h.Write(b) sum := h.Sum(nil) r.Header().Add(digestHeader, - fmt.Sprintf("%s%s%s", - a, - digestDelim, - base64.StdEncoding.EncodeToString(sum[:]))) + string(algo)+ + digestDelim+ + base64.StdEncoding.EncodeToString(sum[:])) return } |
