Class: Pandoru::Transport::BlowfishCryptor
- Inherits:
-
Object
- Object
- Pandoru::Transport::BlowfishCryptor
- Defined in:
- lib/pandoru/transport.rb
Overview
Blowfish cryptography for Pandora API encryption/decryption
Constant Summary collapse
- BLOCK_SIZE =
8
Class Method Summary collapse
Instance Method Summary collapse
- #decrypt(data, strip_padding: true) ⇒ Object
- #encrypt(data) ⇒ Object
-
#initialize(key) ⇒ BlowfishCryptor
constructor
A new instance of BlowfishCryptor.
Constructor Details
#initialize(key) ⇒ BlowfishCryptor
Returns a new instance of BlowfishCryptor.
60 61 62 |
# File 'lib/pandoru/transport.rb', line 60 def initialize(key) @cipher = Crypt::Blowfish.new(key) end |
Class Method Details
.strip_padding(data) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/pandoru/transport.rb', line 93 def self.strip_padding(data) pad_size = data[-1].ord computed_padding = pad_size.chr * pad_size raise ArgumentError, "Invalid padding" unless data[-pad_size..-1] == computed_padding data[0...-pad_size] end |
Instance Method Details
#decrypt(data, strip_padding: true) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/pandoru/transport.rb', line 74 def decrypt(data, strip_padding: true) return '' if data.empty? decoded_data = decode_hex(data) # Decrypt block by block (decrypt_string only does one block!) blocks = decoded_data.scan(/.{#{BLOCK_SIZE}}/m) decrypted = blocks.map { |block| @cipher.decrypt_block(block) }.join strip_padding ? self.class.strip_padding(decrypted) : decrypted end |
#encrypt(data) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/pandoru/transport.rb', line 64 def encrypt(data) padded_data = add_padding(data) # Encrypt block by block for consistency with decrypt blocks = padded_data.scan(/.{#{BLOCK_SIZE}}/m) encrypted = blocks.map { |block| @cipher.encrypt_block(block) }.join encode_hex(encrypted) end |