Class: Dapp::Deployment::Secret

Inherits:
Object
  • Object
show all
Defined in:
lib/dapp/deployment/secret.rb

Defined Under Namespace

Classes: Error, ExtractionError, InvalidKeyError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Secret

Returns a new instance of Secret.



6
7
8
9
# File 'lib/dapp/deployment/secret.rb', line 6

def initialize(key)
  self.class._validate_key!(key)
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



4
5
6
# File 'lib/dapp/deployment/secret.rb', line 4

def key
  @key
end

Class Method Details

._binary_to_hex(key) ⇒ Object



65
66
67
# File 'lib/dapp/deployment/secret.rb', line 65

def _binary_to_hex(key)
  key.unpack('H*').first
end

._hex_to_binary(key) ⇒ Object



61
62
63
# File 'lib/dapp/deployment/secret.rb', line 61

def _hex_to_binary(key)
  [key].pack('H*')
end

._openssl_cipherObject



57
58
59
# File 'lib/dapp/deployment/secret.rb', line 57

def _openssl_cipher
  OpenSSL::Cipher::AES.new(128, :CBC)
end

._validate_key!(key) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/dapp/deployment/secret.rb', line 69

def _validate_key!(key)
  # Требуется 128 битный ключ — это 16 байт.
  # Ключ закодирован в hex кодировке для пользователя.
  # 2 hex символа на 1 байт в hex кодировке.
  # Поэтому требуется длина ключа в hex кодировке в 32 символа.
  if key.bytesize < 32
    raise InvalidKeyError, code: :key_length_too_short, data: {required_size: 32}
  end
end

.generate_keyObject



53
54
55
# File 'lib/dapp/deployment/secret.rb', line 53

def generate_key
  _binary_to_hex _openssl_cipher.random_key
end

Instance Method Details

#extract(hexdata) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dapp/deployment/secret.rb', line 23

def extract(hexdata)
  data = self.class._hex_to_binary hexdata.to_s

  iv_size = data.unpack('S').first
  data = data.byteslice(2..-1)
  raise ExtractionError, code: :bad_data, data: {data: hexdata} unless data

  iv = data.byteslice(0, iv_size)
  data = data.byteslice(iv_size..-1)
  raise ExtractionError, code: :bad_data, data: {data: hexdata} unless data

  decipher = self.class._openssl_cipher
  decipher.decrypt
  decipher.key = self.class._hex_to_binary(key)

  begin
    decipher.iv = iv
  rescue OpenSSL::Cipher::CipherError
    raise ExtractionError, code: :bad_data, data: {data: hexdata}
  end

  begin
    value = decipher.update(data) + decipher.final
  rescue OpenSSL::Cipher::CipherError
    raise ExtractionError, code: :bad_data, data: {data: hexdata}
  end
  value.force_encoding('utf-8')
end

#generate(value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dapp/deployment/secret.rb', line 11

def generate(value)
  cipher = self.class._openssl_cipher
  cipher.encrypt
  cipher.key = self.class._hex_to_binary key
  iv = cipher.random_iv

  iv_size_prefix = [iv.bytesize].pack('S')
  encrypted = cipher.update(value.to_s) + cipher.final

  self.class._binary_to_hex "#{iv_size_prefix}#{iv}#{encrypted}"
end