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

Class Method Details

.base64_to_num(s) ⇒ Object

Decode a base64 byte string to a number.



93
94
95
# File 'lib/openid/cryptutil.rb', line 93

def CryptUtil.base64_to_num(s)
  return binary_to_num(OpenID::Util.from_base64(s))
end

.binary_to_num(s) ⇒ Object

Convert a string of bytes into a number.



76
77
78
79
80
81
82
83
84
85
# File 'lib/openid/cryptutil.rb', line 76

def CryptUtil.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
  return num
end

.hmac_sha1(key, text) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/openid/cryptutil.rb', line 32

def CryptUtil.hmac_sha1(key, text)
  if Digest.const_defined? :HMAC      
    Digest::HMAC.new(key,Digest::SHA1).update(text).digest
  else
    return HMAC::SHA1.digest(key, text)
  end
end

.hmac_sha256(key, text) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/openid/cryptutil.rb', line 44

def CryptUtil.hmac_sha256(key, text)
  if Digest.const_defined? :HMAC      
    Digest::HMAC.new(key,Digest::SHA256).update(text).digest
  else
    return HMAC::SHA256.digest(key, text)
  end
end

.num_to_base64(l) ⇒ Object

Encode a number as a base64-encoded byte string.



88
89
90
# File 'lib/openid/cryptutil.rb', line 88

def CryptUtil.num_to_base64(l)
  return 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.



68
69
70
71
72
73
# File 'lib/openid/cryptutil.rb', line 68

def CryptUtil.num_to_binary(n)
  bits = n.to_s(2)
  prepend = (8 - bits.length % 8)
  bits = ('0' * prepend) + bits
  return [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.



23
24
25
26
# File 'lib/openid/cryptutil.rb', line 23

def CryptUtil.rand(max)
  Kernel.srand()
  return 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.



55
56
57
58
59
60
61
62
63
64
# File 'lib/openid/cryptutil.rb', line 55

def CryptUtil.random_string(length, chars=nil)
  s = ""

  unless chars.nil?
    length.times { s << chars[rand(chars.length)] }
  else
    length.times { s << rand(256).chr }
  end
  return s
end

.sha1(text) ⇒ Object



28
29
30
# File 'lib/openid/cryptutil.rb', line 28

def CryptUtil.sha1(text)
  return Digest::SHA1.digest(text)
end

.sha256(text) ⇒ Object



40
41
42
# File 'lib/openid/cryptutil.rb', line 40

def CryptUtil.sha256(text)
  return Digest::SHA256.digest(text)
end