Created
August 5, 2021 11:12
-
-
Save ArnauMontagud/27993127c266b4c53c5e49dbe2c011f6 to your computer and use it in GitHub Desktop.
[Shell XOR function of a hexadecimal string] #Shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# BASH function to get the result | |
# of a ^ b when a, b are in the | |
# following hexadecimal string | |
# form: AF396463D8705 ... | |
# Obtained from here: | |
# http://www.codeproject.com/Tips/470308/XOR-Hex-Strings-in-Linux-Shell-Script | |
# Author is Sanjay1982 (see http://www.codeproject.com/Members/Sanjay1982) | |
# Usage: | |
# $ xor AB20FF40 DD14FABC | |
function xor() | |
{ | |
local res=(`echo "$1" | sed "s/../0x& /g"`) | |
shift 1 | |
while [[ "$1" ]]; do | |
local one=(`echo "$2" | sed "s/../0x& /g"`) | |
local count1=${#res[@]} | |
if [ $count1 -lt ${#one[@]} ] | |
then | |
count1=${#one[@]} | |
fi | |
for (( i = 0; i < $count1; i++ )) | |
do | |
res[$i]=$((${one[$i]:-0} ^ ${res[$i]:-0})) | |
done | |
shift 1 | |
done | |
printf "%02x" "${res[@]}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment