Module: OpenID::CryptUtil
- Defined in:
- lib/openid/cryptutil.rb
Overview
This module contains everything needed to perform low-level cryptograph and data manipulation tasks.
Class Method Summary collapse
-
.base64_to_num(s) ⇒ Object
Decode a base64 byte string to a number.
-
.binary_to_num(s) ⇒ Object
Convert a string of bytes into a number.
- .const_eq(s1, s2) ⇒ Object
- .hmac_sha1(key, text) ⇒ Object
- .hmac_sha256(key, text) ⇒ Object
-
.num_to_base64(l) ⇒ Object
Encode a number as a base64-encoded byte string.
-
.num_to_binary(n) ⇒ Object
Convert a number to its binary representation; return a string of bytes.
-
.rand(max) ⇒ Object
Generate a random number, doing a little extra work to make it more likely that it’s suitable for cryptography.
-
.random_string(length, chars = nil) ⇒ Object
Generate a random string of the given length, composed of the specified characters.
- .sha1(text) ⇒ Object
- .sha256(text) ⇒ Object
Class Method Details
.base64_to_num(s) ⇒ Object
Decode a base64 byte string to a number.
98 99 100 |
# File 'lib/openid/cryptutil.rb', line 98 def self.base64_to_num(s) binary_to_num(OpenID::Util.from_base64(s)) end |
.binary_to_num(s) ⇒ Object
Convert a string of bytes into a number.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/openid/cryptutil.rb', line 81 def self.binary_to_num(s) # taken from openid-ruby 0.0.1 s = "\000" * (4 - (s.length % 4)) + s num = 0 s.unpack("N*").each do |x| num <<= 32 num |= x end num end |
.const_eq(s1, s2) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/openid/cryptutil.rb', line 102 def self.const_eq(s1, s2) return false if s1.length != s2.length result = true s1.length.times do |i| result &= (s1[i] == s2[i]) end result end |
.hmac_sha1(key, text) ⇒ Object
41 42 43 44 45 |
# File 'lib/openid/cryptutil.rb', line 41 def self.hmac_sha1(key, text) return HMAC::SHA1.digest(key, text) unless defined? OpenSSL OpenSSL::HMAC.digest(OpenSSL::Digest.new("SHA1"), key, text) end |
.hmac_sha256(key, text) ⇒ Object
51 52 53 54 55 |
# File 'lib/openid/cryptutil.rb', line 51 def self.hmac_sha256(key, text) return HMAC::SHA256.digest(key, text) unless defined? OpenSSL OpenSSL::HMAC.digest(OpenSSL::Digest.new("SHA256"), key, text) end |
.num_to_base64(l) ⇒ Object
Encode a number as a base64-encoded byte string.
93 94 95 |
# File 'lib/openid/cryptutil.rb', line 93 def self.num_to_base64(l) OpenID::Util.to_base64(num_to_binary(l)) end |
.num_to_binary(n) ⇒ Object
Convert a number to its binary representation; return a string of bytes.
73 74 75 76 77 78 |
# File 'lib/openid/cryptutil.rb', line 73 def self.num_to_binary(n) bits = n.to_s(2) prepend = (8 - bits.length % 8) bits = ("0" * prepend) + bits [bits].pack("B*") end |
.rand(max) ⇒ Object
Generate a random number, doing a little extra work to make it more likely that it’s suitable for cryptography. If your system doesn’t have /dev/urandom then this number is not cryptographically safe. See <www.cosine.org/2007/08/07/security-ruby-kernel-rand/> for more information. max is the largest possible value of such a random number, where the result will be less than max.
32 33 34 35 |
# File 'lib/openid/cryptutil.rb', line 32 def self.rand(max) Kernel.srand Kernel.rand(max) end |
.random_string(length, chars = nil) ⇒ Object
Generate a random string of the given length, composed of the specified characters. If chars is nil, generate a string composed of characters in the range 0..255.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/openid/cryptutil.rb', line 60 def self.random_string(length, chars = nil) s = "" if chars.nil? length.times { s << rand(256).chr } else length.times { s << chars[rand(chars.length)] } end s end |
.sha1(text) ⇒ Object
37 38 39 |
# File 'lib/openid/cryptutil.rb', line 37 def self.sha1(text) Digest::SHA1.digest(text) end |
.sha256(text) ⇒ Object
47 48 49 |
# File 'lib/openid/cryptutil.rb', line 47 def self.sha256(text) Digest::SHA256.digest(text) end |