67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package xor
|
|
|
|
import "math"
|
|
|
|
/* (1) bitwise XOR between bytes
|
|
*
|
|
* @_left<byte> Left operand
|
|
* @_right<byte> Right operand
|
|
*
|
|
* @return out<byte> Bitwise XOR result between _left and _right
|
|
*
|
|
---------------------------------------------------------*/
|
|
func Byte(_left byte, _right byte) byte {
|
|
|
|
return _left ^ _right
|
|
|
|
}
|
|
|
|
/* (2) bitwise XOR between Byte arrays
|
|
*
|
|
* @_left<byte[]> Left operand
|
|
* @_right<byte[]> Right operand
|
|
*
|
|
* @return out<byte[]> Bitwise XOR result between _left and _right
|
|
*
|
|
* @@ NOTE @@
|
|
* If an argument is smaller, it will be right-padded with null bytes (0x00)
|
|
*
|
|
---------------------------------------------------------*/
|
|
func ByteArray(_left []byte, _right []byte) []byte {
|
|
|
|
/* (1) Process
|
|
---------------------------------------------------------*/
|
|
/* (1) Extract lengths */
|
|
ll := len(_left)
|
|
lr := len(_right)
|
|
|
|
/* (2) Get max length */
|
|
l := int(math.Max(float64(ll), float64(lr)))
|
|
|
|
/* (3) Initialise 'out' */
|
|
out := make([]byte, l, l)
|
|
|
|
/* (2) Process bitwise XOR */
|
|
for i := 0; i < l; i++ {
|
|
|
|
// 1. Out of range for _left
|
|
if i >= ll {
|
|
|
|
out[i] = _right[i]
|
|
|
|
} else if i >= lr {
|
|
|
|
out[i] = _left[i]
|
|
|
|
} else {
|
|
|
|
out[i] = Byte(_left[i], _right[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|