Class: Cryptor::SecretKey

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptor/secret_key.rb

Overview

Secret key used to encrypt plaintexts

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri_string) ⇒ Cryptor::SecretKey

Create a new SecretKey object from a URI

Parameters:

  • uri (#to_s)

    representing a secret key

Raises:

  • (ArgumentError)

    on invalid URIs



30
31
32
33
34
35
36
37
38
39
# File 'lib/cryptor/secret_key.rb', line 30

def initialize(uri_string)
  uri = URI.parse(uri_string.to_s)
  fail ArgumentError, "invalid scheme: #{uri.scheme}" unless uri.scheme == 'secret.key'

  components = uri.path.match(/^\/([^;]+);(.+)$/)
  fail ArgumentError, "couldn't parse cipher name from secret URI" unless components

  @cipher     = Cryptor::Cipher[components[1]]
  @secret_key = Cryptor::Encoding.decode(components[2])
end

Instance Attribute Details

#cipherObject (readonly)

Returns the value of attribute cipher.



7
8
9
# File 'lib/cryptor/secret_key.rb', line 7

def cipher
  @cipher
end

Class Method Details

.random_key(cipher) ⇒ Cryptor::SecretKey

Generate a random secret key

Parameters:

  • Cryptor::Cipher (Cryptor::Cipher, Symbol)

    or algorithm name as a symbol

Returns:



14
15
16
17
18
19
20
21
# File 'lib/cryptor/secret_key.rb', line 14

def self.random_key(cipher)
  cipher = Cryptor::Cipher[cipher] if cipher.is_a? Symbol
  fail ArgumentError, "invalid cipher: #{cipher.inspect}" unless cipher.is_a? Cryptor::Cipher
  bytes  = RbNaCl::Random.random_bytes(cipher.key_bytes)
  base64 = Cryptor::Encoding.encode(bytes)

  new "secret.key:///#{cipher.algorithm};#{base64}"
end

Instance Method Details

#decrypt(ciphertext) ⇒ String

Decrypt ciphertext using this key

Parameters:

  • ciphertext (String)

    string to be decrypted

Returns:

  • (String)

    plaintext decrypted from the given ciphertext



70
71
72
# File 'lib/cryptor/secret_key.rb', line 70

def decrypt(ciphertext)
  @cipher.decrypt(@secret_key, ciphertext)
end

#encrypt(plaintext) ⇒ String

Encrypt a plaintext under this key

Parameters:

  • plaintext (String)

    string to be encrypted

Returns:

  • (String)

    ciphertext encrypted under this key



61
62
63
# File 'lib/cryptor/secret_key.rb', line 61

def encrypt(plaintext)
  @cipher.encrypt(@secret_key, plaintext)
end

#fingerprintString

Fingerprint of this key’s secret URI

Returns:

  • (String)

    fingerprint as a ni:// URL



51
52
53
54
# File 'lib/cryptor/secret_key.rb', line 51

def fingerprint
  digest = Digest::SHA256.digest(to_secret_uri)
  "ni:///sha-256;#{Cryptor::Encoding.encode(digest)}"
end

#inspectString

Inspect this key

Returns:

  • (String)

    a string representing this key



77
78
79
80
81
# File 'lib/cryptor/secret_key.rb', line 77

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} " \
  "cipher=#{cipher.algorithm} " \
  "fingerprint=#{fingerprint}>"
end

#to_secret_uriString

Serialize SecretKey object to a URI

Returns:

  • (String)

    serialized URI representing the key



44
45
46
# File 'lib/cryptor/secret_key.rb', line 44

def to_secret_uri
  "secret.key:///#{@cipher.algorithm};#{Cryptor::Encoding.encode(@secret_key)}"
end