aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/crypto
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2013-02-07 13:31:53 -0800
committerIan Lance Taylor <iant@golang.org>2013-02-07 13:31:53 -0800
commit2b44c33b4a9ca2fa25c88e85803daa57f1080a03 (patch)
tree912539a890028651719516ec99ff3d87e90720e1 /src/pkg/crypto
parent514f10b9ab59c3aa7689fad9c22557f7037e8c42 (diff)
downloadgo-2b44c33b4a9ca2fa25c88e85803daa57f1080a03.tar.xz
crypto/md5: fix for big-endian processors
R=golang-dev, minux.ma, agl CC=golang-dev https://golang.org/cl/7305059
Diffstat (limited to 'src/pkg/crypto')
-rw-r--r--src/pkg/crypto/md5/md5block.go14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/pkg/crypto/md5/md5block.go b/src/pkg/crypto/md5/md5block.go
index 59f8f6f5af..0ca4217740 100644
--- a/src/pkg/crypto/md5/md5block.go
+++ b/src/pkg/crypto/md5/md5block.go
@@ -5,6 +5,16 @@ import (
"unsafe"
)
+const x86 = runtime.GOARCH == "amd64" || runtime.GOARCH == "386"
+
+var littleEndian bool
+
+func init() {
+ x := uint32(0x04030201)
+ y := [4]byte{0x1, 0x2, 0x3, 0x4}
+ littleEndian = *(*[4]byte)(unsafe.Pointer(&x)) == y
+}
+
func block(dig *digest, p []byte) {
a := dig.s[0]
b := dig.s[1]
@@ -16,13 +26,13 @@ func block(dig *digest, p []byte) {
aa, bb, cc, dd := a, b, c, d
// This is a constant condition - it is not evaluated on each iteration.
- if runtime.GOARCH == "amd64" || runtime.GOARCH == "386" {
+ if x86 {
// MD5 was designed so that x86 processors can just iterate
// over the block data directly as uint32s, and we generate
// less code and run 1.3x faster if we take advantage of that.
// My apologies.
X = (*[16]uint32)(unsafe.Pointer(&p[0]))
- } else if uintptr(unsafe.Pointer(&p[0]))&(unsafe.Alignof(uint32(0))-1) == 0 {
+ } else if littleEndian && uintptr(unsafe.Pointer(&p[0]))&(unsafe.Alignof(uint32(0))-1) == 0 {
X = (*[16]uint32)(unsafe.Pointer(&p[0]))
} else {
X = &xbuf