Class: DynamicImage::DigestVerifier

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_image/digest_verifier.rb

Overview

DynamicImage Digest Verifier

Usage

verifier = DynamicImage::DigestVerifier.new("super secret!")

digest = verifier.generate("foo")

digest.verify("foo", digest)
# => true
digest.verify("bar", digest)
# => raises DynamicImage::Errors::InvalidSignature

Credit where credit is due: adapted and simplified from ActiveSupport::MessageVerifier, since we don't need to handle arbitrary data structures and ship the serialized data to the client.

Instance Method Summary collapse

Constructor Details

#initialize(secret, options = {}) ⇒ DigestVerifier


21
22
23
24
# File 'lib/dynamic_image/digest_verifier.rb', line 21

def initialize(secret, options = {})
  @secret = secret
  @digest = options[:digest] || "SHA1"
end

Instance Method Details

#generate(data) ⇒ Object

Generates a digest for a string.


27
28
29
# File 'lib/dynamic_image/digest_verifier.rb', line 27

def generate(data)
  generate_digest(data)
end

#verify(data, digest) ⇒ Object

Verifies that digest is valid for data. Raises a DynamicImage::Errors::InvalidSignature error if not.


33
34
35
36
37
38
39
# File 'lib/dynamic_image/digest_verifier.rb', line 33

def verify(data, digest)
  if valid_digest?(data, digest)
    true
  else
    raise DynamicImage::Errors::InvalidSignature
  end
end