Class: Travis::Encrypt::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/travis/encrypt/base.rb

Direct Known Subclasses

Decryptor, Encryptor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, options) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
# File 'lib/travis/encrypt/base.rb', line 6

def initialize(string, options)
  @string  = string
  @key     = options[:key]
  @options = options
  validate
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



4
5
6
# File 'lib/travis/encrypt/base.rb', line 4

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/travis/encrypt/base.rb', line 4

def options
  @options
end

#stringObject (readonly)

Returns the value of attribute string.



4
5
6
# File 'lib/travis/encrypt/base.rb', line 4

def string
  @string
end

Instance Method Details

#add_iv(string, iv) ⇒ Object



26
27
28
# File 'lib/travis/encrypt/base.rb', line 26

def add_iv(string, iv)
  "#{string}#{iv}"
end

#create_aes(mode = :encrypt, key, iv) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/travis/encrypt/base.rb', line 13

def create_aes(mode = :encrypt, key, iv)
  key = key[0, 32] # https://github.com/ruby/ruby/commit/ce635262f53b760284d56bb1027baebaaec175d1
  aes = OpenSSL::Cipher::AES.new(256, :CBC)
  aes.send(mode)
  aes.key = key
  aes.iv  = iv
  aes
end

#create_ivObject



22
23
24
# File 'lib/travis/encrypt/base.rb', line 22

def create_iv
  SecureRandom.hex(8)
end

#decode(str) ⇒ Object



38
39
40
# File 'lib/travis/encrypt/base.rb', line 38

def decode(str)
  Base64.strict_decode64(str)
end

#encode(str) ⇒ Object



34
35
36
# File 'lib/travis/encrypt/base.rb', line 34

def encode(str)
  Base64.strict_encode64(str)
end

#extract_iv(string) ⇒ Object



30
31
32
# File 'lib/travis/encrypt/base.rb', line 30

def extract_iv(string)
  [string[-16..-1], string[0..-17]]
end