Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
#
# Create a variable `$key` for use in powershell as a AES encyrption key
# constant with (ConvertFrom-SecureString)[https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/convertfrom-securestring?view=powershell-7.2)]
#
# param: $1 secret key (optional, default: secret)
# param: $2 cipher (optional, default: aes-256-cbc)
# output: An byte array (in decimals), the size of the encryption key (AES256 = 32 bytes)
genkey() {
openssl enc -"${2:-aes-256-cbc}" -k "${1:-secret}" -iter +100 -P -md sha1 | \
awk -F'=' '/key/{print $2}' | \
sed -re 's/(.)(.)/\1\2\n/g' | \
while read -r HEX 2>/dev/null; do
test -z "$HEX" || echo $(( 16#$HEX ))
done
return 0
}
if key=("$(genkey "$@")"); then
echo "${key[@]}" | tr '\n' ',' | sed -r 's/^(.*),$/\$key = @\(\1\)/'
echo
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment