github.com/iDigitalFlame/xmt@v0.5.4/data/crypto/subtle/c_no_xor.go (about) 1 //go:build go1.20 2 // +build go1.20 3 4 // Copyright (C) 2020 - 2023 iDigitalFlame 5 // 6 // This program is free software: you can redistribute it and/or modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // any later version. 10 // 11 // This program is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with this program. If not, see <https://www.gnu.org/licenses/>. 18 // 19 20 // Package subtle is similar to the 'cipher/subtle', only needed for very specific 21 // crypto operations. 22 package subtle 23 24 import "crypto/subtle" 25 26 // XorOp will call the 'XorBytes' function but write the value back to the value 27 // instead. This one assumes that the key value is less than or equal to the 28 // value. 29 // 30 // If you need finer control, use the 'XorBytes' function. 31 func XorOp(value, key []byte) { 32 if len(key) == 0 || len(value) == 0 { 33 return 34 } 35 if len(key) == len(value) { 36 subtle.XORBytes(value, key, value) 37 return 38 } 39 for n := 0; n < len(value); { 40 n += subtle.XORBytes(value[n:], key, value[n:]) 41 } 42 } 43 44 // XorBytes is the runtime import from "crypto/cipher.xorBytes" that can use 45 // hardware instructions for a 200% faster XOR operation. 46 // 47 // This variant will overlap the xor for the entire backing array, depending 48 // on which one is larger. 49 func XorBytes(dst, a, b []byte) int { 50 var n int 51 if len(a) < len(b) { 52 for n < len(b) { 53 n += subtle.XORBytes(dst[n:], a, b[n:]) 54 } 55 return n 56 } 57 for n < len(a) { 58 n += subtle.XORBytes(dst[n:], b, a[n:]) 59 } 60 return n 61 }