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