Class: Zold::Key
- Inherits:
-
Object
- Object
- Zold::Key
- Defined in:
- lib/zold/key.rb
Overview
A key
Instance Method Summary collapse
-
#initialize(file: nil, text: nil) ⇒ Key
constructor
A new instance of Key.
- #sign(text) ⇒ Object
- #to_pub ⇒ Object
- #to_s ⇒ Object
- #verify(signature, text) ⇒ Object
Constructor Details
#initialize(file: nil, text: nil) ⇒ Key
Returns a new instance of Key.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/zold/key.rb', line 33 def initialize(file: nil, text: nil) @body = lambda do unless file.nil? path = File.(file) unless File.exist?(path) raise "Can't find RSA key at #{file} (#{path})" end return File.read(path) end unless text.nil? return [ '-----BEGIN PUBLIC KEY-----', text.gsub(/(?<=\G.{64})/, "\n"), '-----END PUBLIC KEY-----' ].join("\n") end raise 'Either file or text must be set' end end |
Instance Method Details
#sign(text) ⇒ Object
61 62 63 |
# File 'lib/zold/key.rb', line 61 def sign(text) Base64.encode64(rsa.sign(OpenSSL::Digest::SHA256.new, text)).delete("\n") end |
#to_pub ⇒ Object
57 58 59 |
# File 'lib/zold/key.rb', line 57 def to_pub to_s.delete("\n").gsub(/-{5}[ A-Z]+-{5}/, '') end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/zold/key.rb', line 53 def to_s rsa.to_s.strip end |
#verify(signature, text) ⇒ Object
65 66 67 |
# File 'lib/zold/key.rb', line 65 def verify(signature, text) rsa.verify(OpenSSL::Digest::SHA256.new, Base64.decode64(signature), text) end |