Class: OpenSesame::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/open-sesame.rb

Overview

Used for passing messages that cannot be altered. This is not for hiding a message from observers. The message is cryptographically signed but it is not encrypted.

Constant Summary collapse

@@default_secret =
"OPEN SESAME"

Class Method Summary collapse

Class Method Details

.generate(message, secret = @@default_secret) ⇒ Object

Generate a message string that can be verified by another computer that knows the shared secret.

Parameters:

  • message (String)

    The message.

  • secret (String) (defaults to: @@default_secret)

    The shared secret phrase.


47
48
49
50
# File 'lib/open-sesame.rb', line 47

def self.generate(message, secret =  @@default_secret)
  hash = (Digest::SHA1.new << secret + message).to_s
  message + '-' + hash
end

.message(message, secret = @@default_secret) ⇒ Object

Verify a message that was generated with OpenSesame and return just the message part. Returns nil if the message does not verify.

Parameters:

  • message (String)

    The message that was generated by OpenSesame, which includes a cryptographic hash.

  • secret (String) (defaults to: @@default_secret)

    The secret to use for verifying the message.


67
68
69
70
71
72
73
# File 'lib/open-sesame.rb', line 67

def self.message(message, secret = @@default_secret)
  if self.verify(message, secret)
    (message.split /-/).first
  else
    nil
  end
end

.verify(message, secret = @@default_secret) ⇒ Object

Verify that the message has not been altered.

Parameters:

  • message (String)

    The message to verify.

  • secret (String) (defaults to: @@default_secret)

    The shared secret phrase.


56
57
58
59
# File 'lib/open-sesame.rb', line 56

def self.verify(message, secret = @@default_secret)
  string = message.split /-/
  message.eql? generate(string.first, secret)
end