2 # sha2.rb - defines Digest::SHA2 class which wraps up the SHA256,
3 # SHA384, and SHA512 classes.
5 # Copyright (c) 2006 Akinori MUSHA <knu@iDaemons.org>
7 # All rights reserved. You can redistribute and/or modify it under the same
10 # $Id: sha2.rb 11708 2007-02-12 23:01:19Z shyouhei $
13 require 'ext/digest/sha2/sha2'
15 Digest.create :SHA256, 'rbx_Digest_SHA256_Init', 'rbx_Digest_SHA256_Update',
16 'rbx_Digest_SHA256_Finish', (4 * 8 + 8 + 64), 64, 32
18 Digest.create :SHA384, 'rbx_Digest_SHA384_Init', 'rbx_Digest_SHA384_Update',
19 'rbx_Digest_SHA384_Finish', (8 * 8 + 8 * 2 + 128), 128, 48
21 Digest.create :SHA512, 'rbx_Digest_SHA512_Init', 'rbx_Digest_SHA512_Update',
22 'rbx_Digest_SHA512_Finish', (8 * 8 + 8 * 2 + 128), 128, 64
26 # A meta digest provider class for SHA256, SHA384 and SHA512.
33 # Digest::SHA2.new(bitlen = 256) -> digest_obj
35 # Creates a new SHA2 hash object with a given bit length.
36 def initialize(bitlen = 256)
39 @sha2 = Digest::SHA256.new
41 @sha2 = Digest::SHA384.new
43 @sha2 = Digest::SHA512.new
45 raise ArgumentError, "unsupported bit length: %s" % bitlen.inspect
77 def initialize_copy(other)
78 @sha2 = other.instance_eval { @sha2.clone }
83 "#<%s:%d %s>" % [self.class.name, @bitlen, hexdigest]