PageRenderTime 39ms CodeModel.GetById 30ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/openssl/digest.rb

http://github.com/agross/netopenspace
Ruby | 61 lines | 33 code | 8 blank | 20 comment | 2 complexity | e0259e282eae18fcab5ea309d37221dd MD5 | raw file
 1=begin
 2= $RCSfile$ -- Ruby-space predefined Digest subclasses
 3
 4= Info
 5  'OpenSSL for Ruby 2' project
 6  Copyright (C) 2002  Michal Rokos <m.rokos@sh.cvut.cz>
 7  All rights reserved.
 8
 9= Licence
10  This program is licenced under the same licence as Ruby.
11  (See the file 'LICENCE'.)
12
13= Version
14  $Id: digest.rb 28004 2010-05-24 23:58:49Z shyouhei $
15=end
16
17##
18# Should we care what if somebody require this file directly?
19#require 'openssl'
20
21module OpenSSL
22  class Digest
23
24    alg = %w(DSS DSS1 MD2 MD4 MD5 MDC2 RIPEMD160 SHA SHA1)
25    if OPENSSL_VERSION_NUMBER > 0x00908000
26      alg += %w(SHA224 SHA256 SHA384 SHA512)
27    end
28
29    def self.digest(name, data)
30        super(data, name)
31    end
32
33    alg.each{|name|
34      klass = Class.new(Digest){
35        define_method(:initialize){|*data|
36          if data.length > 1
37            raise ArgumentError,
38              "wrong number of arguments (#{data.length} for 1)"
39          end
40          super(name, data.first)
41        }
42      }
43      singleton = (class << klass; self; end)
44      singleton.class_eval{
45        define_method(:digest){|data| Digest.digest(name, data) }
46        define_method(:hexdigest){|data| Digest.hexdigest(name, data) }
47      }
48      const_set(name, klass)
49    }
50
51    # This class is only provided for backwards compatibility.  Use OpenSSL::Digest in the future.
52    class Digest < Digest
53      def initialize(*args)
54        # add warning
55        super(*args)
56      end
57    end
58
59  end # Digest
60end # OpenSSL
61