Skip to content

Instantly share code, notes, and snippets.

@ambakshi
Created December 31, 2021 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ambakshi/b2501d6a765774221afdeaa0706a0d18 to your computer and use it in GitHub Desktop.
Save ambakshi/b2501d6a765774221afdeaa0706a0d18 to your computer and use it in GitHub Desktop.
#!/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