Module: OpenID::CryptUtil

### Home Classes Methods

### In Files

- lib/openid/cryptutil.rb

### Methods

- ::base64_to_num
- ::binary_to_num
- ::const_eq
- ::hmac_sha1
- ::hmac_sha256
- ::num_to_base64
- ::num_to_binary
- ::rand
- ::random_string
- ::sha1
- ::sha256

### Files

- LICENSE

### Class/Module Index

Quicksearch

- OpenID
- OpenID::AX
- OpenID::AX::AXMessage
- OpenID::AX::AttrInfo
- OpenID::AX::Error
- OpenID::AX::FetchRequest
- OpenID::AX::FetchResponse
- OpenID::AX::KeyValueMessage
- OpenID::AX::StoreRequest
- OpenID::AX::StoreResponse
- OpenID::AssertionError
- OpenID::Association
- OpenID::AssociationNegotiator
- OpenID::Consumer
- OpenID::Consumer::AssociationManager
- OpenID::Consumer::CancelResponse
- OpenID::Consumer::CheckIDRequest
- OpenID::Consumer::DiffieHellmanSHA1Session
- OpenID::Consumer::DiffieHellmanSHA256Session
- OpenID::Consumer::DiffieHellmanSession
- OpenID::Consumer::DiscoveredServices
- OpenID::Consumer::DiscoveryManager
- OpenID::Consumer::FailureResponse
- OpenID::Consumer::IdResHandler
- OpenID::Consumer::NoEncryptionSession
- OpenID::Consumer::Response
- OpenID::Consumer::Session
- OpenID::Consumer::SetupNeededResponse
- OpenID::Consumer::SuccessResponse
- OpenID::CryptUtil
- OpenID::DiffieHellman
- OpenID::DiscoveryFailure
- OpenID::Extension
- OpenID::FetchingError
- OpenID::HTTPRedirectLimitReached
- OpenID::HTTPResponse
- OpenID::HTTPStatusError
- OpenID::InvalidOpenIDNamespace
- OpenID::KVFormError
- OpenID::KVPostNetworkError
- OpenID::Message
- OpenID::Message::KeyNotFound
- OpenID::NamespaceAliasRegistrationError
- OpenID::NamespaceMap
- OpenID::Nonce
- OpenID::OAuth
- OpenID::OAuth::Request
- OpenID::OAuth::Response
- OpenID::OpenIDError
- OpenID::OpenIDServiceEndpoint
- OpenID::PAPE
- OpenID::PAPE::Request
- OpenID::PAPE::Response
- OpenID::ProtocolError
- OpenID::RealmVerificationRedirected
- OpenID::SReg
- OpenID::SReg::NamespaceError
- OpenID::SReg::Request
- OpenID::SReg::Response
- OpenID::SSLFetchingError
- OpenID::Server
- OpenID::Server::AlreadySigned
- OpenID::Server::AssociateRequest
- OpenID::Server::BaseServerSession
- OpenID::Server::CheckAuthRequest
- OpenID::Server::CheckIDRequest
- OpenID::Server::Decoder
- OpenID::Server::DiffieHellmanSHA1ServerSession
- OpenID::Server::DiffieHellmanSHA256ServerSession
- OpenID::Server::Encoder
- OpenID::Server::EncodingError
- OpenID::Server::MalformedReturnURL
- OpenID::Server::MalformedTrustRoot
- OpenID::Server::NoReturnToError
- OpenID::Server::OpenIDRequest
- OpenID::Server::OpenIDResponse
- OpenID::Server::PlainTextServerSession
- OpenID::Server::ProtocolError
- OpenID::Server::Server
- OpenID::Server::Signatory
- OpenID::Server::SigningEncoder
- OpenID::Server::UntrustedReturnURL
- OpenID::Server::VersionError
- OpenID::Server::WebResponse
- OpenID::ServerError
- OpenID::StandardFetcher
- OpenID::Store
- OpenID::Store::Filesystem
- OpenID::Store::Interface
- OpenID::Store::Memcache
- OpenID::Store::Memory
- OpenID::TrustRoot
- OpenID::TrustRoot::TrustRoot
- OpenID::TypeURIMismatch
- OpenID::UI
- OpenID::UI::Request
- OpenID::URINorm
- OpenID::UndefinedOpenIDNamespace
- OpenID::Util
- OpenID::Yadis
- OpenID::Yadis::BasicServiceEndpoint
- OpenID::Yadis::CompoundFilter
- OpenID::Yadis::DiscoveryResult
- OpenID::Yadis::TransformFilterMaker
- OpenID::Yadis::XRDSError
- OpenID::Yadis::XRDSFraud
- OpenID::Yadis::XRI
- OpenID::Yadis::XRI::ProxyResolver
- OpenID::Yadis::XRI::XRIHTTPError
- HMAC
- HMAC::Base
- HMAC::SHA1
- HMAC::SHA256
- HMAC::SHA384
- HMAC::SHA512
- Net
- Net::HTTP
- HTMLComment
- HTMLTag
- HTMLText
- HTMLToken
- HTMLTokenizer
- HTMLTokenizerError
- Object

No matching classes.

# OpenID::CryptUtil

This module contains everything needed to perform low-level cryptograph and data manipulation tasks.

### Public Class Methods

base64_to_num(s) click to toggle source

Decode a base64 byte string to a number.

    # File lib/openid/cryptutil.rb, line 100
    def CryptUtil.base64_to_num(s)
      return binary_to_num(OpenID::Util.from_base64(s))
    end

binary_to_num(s) click to toggle source

Convert a string of bytes into a number.

    # File lib/openid/cryptutil.rb, line 83
    def CryptUtil.binary_to_num(s)
      # taken from openid-ruby 0.0.1
      s = "\0000" * (4 - (s.length % 4)) + s
      num = 0
      s.unpack('N*').each do |x|
        num <<= 32
        num |= x
      end
      return num
    end

const_eq(s1, s2) click to toggle source

    # File lib/openid/cryptutil.rb, line 104
    def CryptUtil.const_eq(s1, s2)
      if s1.length != s2.length
        return false
      end
      result = true
      s1.length.times do |i|
        result &= (s1[i] == s2[i])
      end
      return result
    end

hmac_sha1(key, text) click to toggle source

    # File lib/openid/cryptutil.rb, line 39
    def CryptUtil.hmac_sha1(key, text)
      if defined? OpenSSL
        OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, key, text)
      else
        return HMAC::SHA1.digest(key, text)
      end
    end

hmac_sha256(key, text) click to toggle source

    # File lib/openid/cryptutil.rb, line 51
    def CryptUtil.hmac_sha256(key, text)
      if defined? OpenSSL
        OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, key, text)
      else
        return HMAC::SHA256.digest(key, text)
      end
    end

num_to_base64(l) click to toggle source

Encode a number as a base64-encoded byte string.

    # File lib/openid/cryptutil.rb, line 95
    def CryptUtil.num_to_base64(l)
      return OpenID::Util.to_base64(num_to_binary(l))
    end

num_to_binary(n) click to toggle source

Convert a number to its binary representation; return a string of bytes.

    # File lib/openid/cryptutil.rb, line 75
    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) click to toggle source

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.

    # File lib/openid/cryptutil.rb, line 30
    def CryptUtil.rand(max)
      Kernel.srand()
      return Kernel.rand(max)
    end

random_string(length, chars=nil) click to toggle source

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.

    # File lib/openid/cryptutil.rb, line 62
    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) click to toggle source

    # File lib/openid/cryptutil.rb, line 35
    def CryptUtil.sha1(text)
      return Digest::SHA1.digest(text)
    end

sha256(text) click to toggle source

    # File lib/openid/cryptutil.rb, line 47
    def CryptUtil.sha256(text)
      return Digest::SHA256.digest(text)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.