forked from enceve/crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xor_amd64.go
36 lines (28 loc) · 803 Bytes
/
xor_amd64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Use of this source code is governed by a license
// that can be found in the LICENSE file.
// +build amd64, !cgo, !appengine
package crypto
import "unsafe"
const wordSize = int(unsafe.Sizeof(uintptr(0)))
// XOR xors the bytes in src and with and writes the result to dst.
// The destination is assumed to have enough space. Returns the
// number of bytes xor'd.
func XOR(dst, src, with []byte) int {
n := len(src)
if len(with) < n {
n = len(with)
}
w := n / wordSize
if w > 0 {
dstPtr := *(*[]uintptr)(unsafe.Pointer(&dst))
srcPtr := *(*[]uintptr)(unsafe.Pointer(&src))
withPtr := *(*[]uintptr)(unsafe.Pointer(&with))
for i, v := range srcPtr[:w] {
dstPtr[i] = withPtr[i] ^ v
}
}
for i := (n & (^(wordSize - 1))); i < n; i++ {
dst[i] = src[i] ^ with[i]
}
return n
}