Module: XapoUtils
- Defined in:
- lib/xapo_utils.rb
Class Method Summary collapse
-
.encrypt(payload, secret, default_padding = true) ⇒ Object
Do PKCS#7 padding and encrypting.
- .timestamp ⇒ Object
- .zero_padding(payload) ⇒ Object
Class Method Details
.encrypt(payload, secret, default_padding = true) ⇒ Object
Do PKCS#7 padding and encrypting.
Args:
payload (str): The text to encode.
secret (str): the encoding key.
default_padding (bool): whether it uses default padding or not
Returns:
str: The padded bytestring.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/xapo_utils.rb', line 17 def encrypt(payload, secret, default_padding=true) cipher = OpenSSL::Cipher::AES.new("256-ECB") cipher.encrypt cipher.key = secret # TODO zero padding is not handled correctly, it's too specific # and inflexible making it hard to change. if !default_padding cipher.padding = 0 payload = zero_padding(payload) end encrypted = cipher.update(payload) + cipher.final return Base64.encode64(encrypted) end |
.timestamp ⇒ Object
34 |
# File 'lib/xapo_utils.rb', line 34 def ; (Time.now.to_f * 1000).to_i end |
.zero_padding(payload) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/xapo_utils.rb', line 36 def zero_padding(payload) l = 16 - payload.length % 16 res = payload + "\0" * l return res end |