lib/xapo_utils.rb in xapo_sdk-0.2.0 vs lib/xapo_utils.rb in xapo_sdk-0.2.1

- old
+ new

@@ -6,22 +6,37 @@ module_function # Do PKCS#7 padding and encrypting. # # Args: - # bytestring (str): The text to encode. - # k (int, optional): The padding block size. It defaults to k=16. - # + # 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. - def encrypt(payload, secret) + def encrypt(payload, secret, default_padding=true) cipher = OpenSSL::Cipher::AES.new("256-ECB") cipher.encrypt - cipher.key = secret + 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 def timestamp; (Time.now.to_f * 1000).to_i end + + def zero_padding(payload) + l = 16 - payload.length % 16 + res = payload + "\0" * l + + return res + end end \ No newline at end of file