Skip to content

Instantly share code, notes, and snippets.

@joshk0
Last active August 29, 2015 14:16
Show Gist options
  • Save joshk0/374f439ecf1242653529 to your computer and use it in GitHub Desktop.
Save joshk0/374f439ecf1242653529 to your computer and use it in GitHub Desktop.
PWM encrypt/decrypt in Ruby
# This ruby script lets you encrypt and decrypt secrets stored in PwmConfiguration.xml files.
# The secret key consists of the 'createTime' attribute of the root element of the file, concatenated
# with the string StoredConfiguration. You can use this to auto generate compliant PwmConfiguration.xml
# files if you control the createTime.
require 'base64'
require 'openssl'
require 'digest/sha1'
def pwm_cipher
OpenSSL::Cipher::Cipher.new('AES-128-ECB')
end
def pwm_make_key(key)
# pwm keeps only the first 16 bytes
Digest::SHA1.digest(key).byteslice(0..-5)
end
def pwm_encrypt(key, plain)
aes = pwm_cipher
aes.encrypt
aes.key = pwm_make_key(key)
hex = aes.update(plain) + aes.final
Base64.urlsafe_encode64(hex)
end
def pwm_decrypt(key, crypt)
hex = Base64.urlsafe_decode64(crypt)
aes = pwm_cipher
aes.decrypt
aes.key = pwm_make_key(key)
aes.update(hex) + aes.final
end
# try it: puts pwm_decrypt(somekey, pwm_encrypt(somekey, someplaintext))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment