~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/digest/sha2/lib/sha2.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#--
 
2
# sha2.rb - defines Digest::SHA2 class which wraps up the SHA256,
 
3
#           SHA384, and SHA512 classes.
 
4
#++
 
5
# Copyright (c) 2006 Akinori MUSHA <knu@iDaemons.org>
 
6
#
 
7
# All rights reserved.  You can redistribute and/or modify it under the same
 
8
# terms as Ruby.
 
9
#
 
10
#   $Id: sha2.rb 11708 2007-02-12 23:01:19Z shyouhei $
 
11
 
 
12
require 'digest'
 
13
 
 
14
module Digest
 
15
  #
 
16
  # A meta digest provider class for SHA256, SHA384 and SHA512.
 
17
  #
 
18
  class SHA2 < Digest::Class
 
19
    # call-seq:
 
20
    #     Digest::SHA2.new(bitlen = 256) -> digest_obj
 
21
    #
 
22
    # Creates a new SHA2 hash object with a given bit length.
 
23
    def initialize(bitlen = 256)
 
24
      case bitlen
 
25
      when 256
 
26
        @sha2 = Digest::SHA256.new
 
27
      when 384
 
28
        @sha2 = Digest::SHA384.new
 
29
      when 512
 
30
        @sha2 = Digest::SHA512.new
 
31
      else
 
32
        raise ArgumentError, "unsupported bit length: %s" % bitlen.inspect
 
33
      end
 
34
      @bitlen = bitlen
 
35
    end
 
36
 
 
37
    # :nodoc:
 
38
    def reset
 
39
      @sha2.reset
 
40
      self
 
41
    end
 
42
 
 
43
    # :nodoc:
 
44
    def update(str)
 
45
      @sha2.update(str)
 
46
      self
 
47
    end
 
48
    alias << update
 
49
 
 
50
    def finish
 
51
      @sha2.digest!
 
52
    end
 
53
    private :finish
 
54
 
 
55
    def block_length
 
56
      @sha2.block_length
 
57
    end
 
58
 
 
59
    def digest_length
 
60
      @sha2.digest_length
 
61
    end
 
62
 
 
63
    # :nodoc:
 
64
    def initialize_copy(other)
 
65
      @sha2 = other.instance_eval { @sha2.clone }
 
66
    end
 
67
 
 
68
    # :nodoc:
 
69
    def inspect
 
70
      "#<%s:%d %s>" % [self.class.name, @bitlen, hexdigest]
 
71
    end
 
72
  end
 
73
end