Module: Nuckle::Util

Defined in:
lib/nuckle/util.rb

Overview

Constant-time comparison utilities for cryptographic data.

Class Method Summary collapse

Class Method Details

.verify(a, b, expected_size = nil) ⇒ Boolean

Compares two binary strings for equality.

Parameters:

  • a (String)

    first string

  • b (String)

    second string

  • expected_size (Integer, nil) (defaults to: nil)

    required byte length, or nil to skip check

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/nuckle/util.rb', line 30

def self.verify(a, b, expected_size = nil)
  a = a.b if a.encoding != Encoding::BINARY
  b = b.b if b.encoding != Encoding::BINARY
  return false if expected_size && (a.bytesize != expected_size || b.bytesize != expected_size)

  a == b
end

.verify16(a, b) ⇒ Object

Compare two 16-byte strings.



7
8
9
# File 'lib/nuckle/util.rb', line 7

def self.verify16(a, b)
  verify(a, b, 16)
end

.verify32(a, b) ⇒ Object

Compare two 32-byte strings.



13
14
15
# File 'lib/nuckle/util.rb', line 13

def self.verify32(a, b)
  verify(a, b, 32)
end

.verify64(a, b) ⇒ Object

Compare two 64-byte strings.



19
20
21
# File 'lib/nuckle/util.rb', line 19

def self.verify64(a, b)
  verify(a, b, 64)
end