Module: OpenSSL

Defined in:
lib/openssl/bn.rb,
lib/openssl.rb,
lib/openssl.rb,
lib/openssl/ssl.rb,
lib/openssl/hmac.rb,
lib/openssl/x509.rb,
lib/openssl/cipher.rb,
lib/openssl/config.rb,
lib/openssl/digest.rb,
lib/openssl/marshal.rb

Overview

--

Ruby-space definitions to add DER (de)serialization to classes

Info

'OpenSSL for Ruby 2' project Copyright (C) 2002 Michal Rokos [email protected] All rights reserved.

Licence

This program is licensed under the same licence as Ruby. (See the file 'LICENCE'.) ++

Defined Under Namespace

Modules: Buffering, Marshal, PKey, SSL, X509 Classes: BN, Cipher, Config, ConfigError, Digest, HMAC

Class Method Summary collapse

Class Method Details

.Digest(name) ⇒ Object

Returns a Digest subclass by name

require 'openssl'

OpenSSL::Digest("MD5")
# => OpenSSL::Digest::MD5

Digest("Foo")
# => NameError: wrong constant name Foo


67
68
69
# File 'lib/openssl/digest.rb', line 67

def Digest(name)
  OpenSSL::Digest.const_get(name)
end

.secure_compare(a, b) ⇒ Object

:call-seq:

OpenSSL.secure_compare(string, string) -> true or false

Constant time memory comparison. Inputs are hashed using SHA-256 to mask the length of the secret. Returns true if the strings are identical, false otherwise.

This method is expensive due to the SHA-256 hashing. In most cases, where the input lengths are known to be equal or are not sensitive, OpenSSL.fixed_length_secure_compare should be used instead.



41
42
43
44
45
# File 'lib/openssl.rb', line 41

def self.secure_compare(a, b)
  hashed_a = OpenSSL::Digest.digest('SHA256', a)
  hashed_b = OpenSSL::Digest.digest('SHA256', b)
  OpenSSL.fixed_length_secure_compare(hashed_a, hashed_b) && a == b
end