aboutsummaryrefslogtreecommitdiff
path: root/src/crypto/internal
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2023-04-02 21:21:24 -0700
committerGopher Robot <gobot@golang.org>2023-04-25 22:05:33 +0000
commitbb079efbdcc738a236b30f295ccb286df58e2bc3 (patch)
tree7cbefc3b2e9f8d315327923d3b135482bfbc16d9 /src/crypto/internal
parentce0b9143123abe8005a059c92dbd763f7ab46214 (diff)
downloadgo-bb079efbdcc738a236b30f295ccb286df58e2bc3.tar.xz
crypto/sha256: add WriteString and WriteByte method
This can reduce allocations when hashing a string or byte rather than []byte. For #38776 Change-Id: I1c6dd1bc018220784a05939e92b47558c0562110 Reviewed-on: https://go-review.googlesource.com/c/go/+/481478 Reviewed-by: Joel Sing <joel@sing.id.au> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/crypto/internal')
-rw-r--r--src/crypto/internal/boring/sha.go14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/crypto/internal/boring/sha.go b/src/crypto/internal/boring/sha.go
index cf82f3f64f..b7843674a5 100644
--- a/src/crypto/internal/boring/sha.go
+++ b/src/crypto/internal/boring/sha.go
@@ -263,6 +263,20 @@ func (h *sha256Hash) Write(p []byte) (int, error) {
return len(p), nil
}
+func (h *sha256Hash) WriteString(s string) (int, error) {
+ if len(s) > 0 && C._goboringcrypto_SHA256_Update(h.noescapeCtx(), unsafe.Pointer(unsafe.StringData(s)), C.size_t(len(s))) == 0 {
+ panic("boringcrypto: SHA256_Update failed")
+ }
+ return len(s), nil
+}
+
+func (h *sha256Hash) WriteByte(c byte) error {
+ if C._goboringcrypto_SHA256_Update(h.noescapeCtx(), unsafe.Pointer(&c), 1) == 0 {
+ panic("boringcrypto: SHA256_Update failed")
+ }
+ return nil
+}
+
func (h0 *sha256Hash) sum(dst []byte) []byte {
h := *h0 // make copy so future Write+Sum is valid
if C._goboringcrypto_SHA256_Final((*C.uint8_t)(noescape(unsafe.Pointer(&h.out[0]))), h.noescapeCtx()) == 0 {