From 1da9ede808ab6f36cc5b648b50896bc5ab552d61 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 19 Apr 2018 15:09:15 +0200 Subject: [PATCH] [bin.xor] created bitwise XOR shorthand for Byte() and ByteArray() --- .gitignore | 4 ++ src/git.xdrm.io/schastsp/bin/xor/xor.go | 67 +++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 .gitignore create mode 100644 src/git.xdrm.io/schastsp/bin/xor/xor.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1daa157 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/bin +/pkg +/src/* +!/src/git.xdrm.io/ \ No newline at end of file diff --git a/src/git.xdrm.io/schastsp/bin/xor/xor.go b/src/git.xdrm.io/schastsp/bin/xor/xor.go new file mode 100644 index 0000000..cc7f1ab --- /dev/null +++ b/src/git.xdrm.io/schastsp/bin/xor/xor.go @@ -0,0 +1,67 @@ +package xor + +import "math" + +/* (1) bitwise XOR between bytes +* +* @_left Left operand +* @_right Right operand +* +* @return out Bitwise XOR result between _left and _right +* +---------------------------------------------------------*/ +func Byte(_left byte, _right byte) byte { + + return _left ^ _right; + +} + + +/* (2) bitwise XOR between Byte arrays +* +* @_left Left operand +* @_right Right operand +* +* @return out 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 + +}