88package bitutil
99
1010import (
11+ "crypto/subtle"
1112 "runtime"
1213 "unsafe"
1314)
@@ -17,46 +18,16 @@ const supportsUnaligned = runtime.GOARCH == "386" || runtime.GOARCH == "amd64" |
1718
1819// XORBytes xors the bytes in a and b. The destination is assumed to have enough
1920// space. Returns the number of bytes xor'd.
21+ //
22+ // If dst does not have length at least n,
23+ // XORBytes panics without writing anything to dst.
24+ //
25+ // dst and x or y may overlap exactly or not at all,
26+ // otherwise XORBytes may panic.
27+ //
28+ // Deprecated: use crypto/subtle.XORBytes
2029func XORBytes (dst , a , b []byte ) int {
21- if supportsUnaligned {
22- return fastXORBytes (dst , a , b )
23- }
24- return safeXORBytes (dst , a , b )
25- }
26-
27- // fastXORBytes xors in bulk. It only works on architectures that support
28- // unaligned read/writes.
29- func fastXORBytes (dst , a , b []byte ) int {
30- n := len (a )
31- if len (b ) < n {
32- n = len (b )
33- }
34- w := n / wordSize
35- if w > 0 {
36- dw := * (* []uintptr )(unsafe .Pointer (& dst ))
37- aw := * (* []uintptr )(unsafe .Pointer (& a ))
38- bw := * (* []uintptr )(unsafe .Pointer (& b ))
39- for i := 0 ; i < w ; i ++ {
40- dw [i ] = aw [i ] ^ bw [i ]
41- }
42- }
43- for i := n - n % wordSize ; i < n ; i ++ {
44- dst [i ] = a [i ] ^ b [i ]
45- }
46- return n
47- }
48-
49- // safeXORBytes xors one by one. It works on all architectures, independent if
50- // it supports unaligned read/writes or not.
51- func safeXORBytes (dst , a , b []byte ) int {
52- n := len (a )
53- if len (b ) < n {
54- n = len (b )
55- }
56- for i := 0 ; i < n ; i ++ {
57- dst [i ] = a [i ] ^ b [i ]
58- }
59- return n
30+ return subtle .XORBytes (dst , a , b )
6031}
6132
6233// ANDBytes ands the bytes in a and b. The destination is assumed to have enough
0 commit comments