Project

General

Profile

Feature #10452 ยป 0001-ext-digest-digest.c.patch

sdaubert (Sylvain Daubert), 10/29/2014 07:23 PM

View differences:

ext/digest/digest.c
77 77
 *
78 78
 * Different digest algorithms (or hash functions) are available:
79 79
 *
80
 * HMAC::
81
 *   See FIPS PUB 198 The Keyed-Hash Message Authentication Code (HMAC).
80
 * MD5::
81
 *  See RFC 1321 The MD5 Message-Digest Algorithm
82 82
 * RIPEMD-160::
83 83
 *   As Digest::RMD160.
84 84
 *   See http://homes.esat.kuleuven.be/~bosselae/ripemd160.html.
......
499 499
 *
500 500
 * This abstract class provides a common interface to message digest
501 501
 * implementation classes written in C.
502
 *
503
 * ==Write a Digest subclass in C
504
 * Digest::Base provides a common interface to message digest
505
 * classes written in C. These classes must provide a struct
506
 * of type rb_digest_metadata_t:
507
 *  typedef int (*rb_digest_hash_init_func_t)(void *);
508
 *  typedef void (*rb_digest_hash_update_func_t)(void *, unsigned char *, size_t);
509
 *  typedef int (*rb_digest_hash_finish_func_t)(void *, unsigned char *);
510
 *  
511
 *  typedef struct {
512
 *    int api_version;
513
 *    size_t digest_len;
514
 *    size_t block_len;
515
 *    size_t ctx_size;
516
 *    rb_digest_hash_init_func_t init_func;
517
 *    rb_digest_hash_update_func_t update_func;
518
 *    rb_digest_hash_finish_func_t finish_func;
519
 *  } rb_digest_metadata_t;
520
 *
521
 * This structure must be set as an instance variable named +metadata+
522
 * (without the +@+ in front of the name). By example:
523
 *   static const rb_digest_metadata_t sha1 = {
524
 *      RUBY_DIGEST_API_VERSION,
525
 *      SHA1_DIGEST_LENGTH,
526
 *      SHA1_BLOCK_LENGTH,
527
 *      sizeof(SHA1_CTX),
528
 *      (rb_digest_hash_init_func_t)SHA1_Init,
529
 *      (rb_digest_hash_update_func_t)SHA1_Update,
530
 *      (rb_digest_hash_finish_func_t)SHA1_Finish,
531
 *  };
532
 *
533
 *
534
 *  rb_ivar_set(cDigest_SHA1, rb_intern("metadata"),
535
 *		Data_Wrap_Struct(0, 0, 0, (void *)&sha1));
502 536
 */
503 537

  
504 538
static rb_digest_metadata_t *
......
595 629
    return copy;
596 630
}
597 631

  
598
/* :nodoc: */
632
/*
633
 * call-seq: digest_base.reset -> digest_base
634
 *
635
 * Reset the digest to its initial state and return +self+.
636
 */
599 637
static VALUE
600 638
rb_digest_base_reset(VALUE self)
601 639
{
......
611 649
    return self;
612 650
}
613 651

  
614
/* :nodoc: */
652
/*
653
 * call-seq: 
654
 *   digest_base.update(string) -> digest_base
655
 *   digest_base << string -> digest_base
656
 *
657
 * Update the digest using given _string_ and return +self+.
658
 */
615 659
static VALUE
616 660
rb_digest_base_update(VALUE self, VALUE str)
617 661
{
......
649 693
    return str;
650 694
}
651 695

  
652
/* :nodoc: */
696
/*
697
 * call-seq: digest_base.digest_length -> Integer
698
 *
699
 * Return the length of the hash value in bytes.
700
 */
653 701
static VALUE
654 702
rb_digest_base_digest_length(VALUE self)
655 703
{
......
660 708
    return INT2NUM(algo->digest_len);
661 709
}
662 710

  
663
/* :nodoc: */
711
/*
712
 * call-seq: digest_base.block_length -> Integer
713
 *
714
 * Return the block length of the digest in bytes.
715
 */
664 716
static VALUE
665 717
rb_digest_base_block_length(VALUE self)
666 718
{
ext/digest/md5/md5init.c
19 19
};
20 20

  
21 21
/*
22
 * Document-class: Digest::MD5 < Digest::Base
22 23
 * A class for calculating message digests using the MD5
23 24
 * Message-Digest Algorithm by RSA Data Security, Inc., described in
24 25
 * RFC1321.
26
 *
27
 * MD5 calculates a digest of 128 bits (16 bytes).
28
 *
29
 * == Examples
30
 *  require 'digest'
31
 *  
32
 *  # Compute a complete digest
33
 *  Digest::MD5.hexdigest 'abc'      #=> "90015098..."
34
 *
35
 *  # Compute digest by chunks
36
 *  md5 = Digest::MD5.new               # =>#<Digest::MD5>
37
 *  md5.update "ab"
38
 *  md5 << "c"                           # alias for #update
39
 *  md5.hexdigest                        # => "90015098..."
40
 *
41
 *  # Use the same object to compute another digest
42
 *  md5.reset
43
 *  md5 << "message"
44
 *  md5.hexdigest                        # => "c13557f2..."
25 45
 */
26 46
void
27 47
Init_md5(void)
ext/digest/rmd160/rmd160init.c
19 19
};
20 20

  
21 21
/*
22
 * Document-class: Digest::RMD160 < Digest::Base
22 23
 * A class for calculating message digests using RIPEMD-160
23 24
 * cryptographic hash function, designed by Hans Dobbertin, Antoon
24 25
 * Bosselaers, and Bart Preneel.
26
 *
27
 * RMD160 calculates a digest of 160 bits (20 bytes).
28
 *
29
 * == Examples
30
 *  require 'digest'
31
 *  
32
 *  # Compute a complete digest
33
 *  Digest::RMD160.hexdigest 'abc'      #=> "8eb208f7..."
34
 *
35
 *  # Compute digest by chunks
36
 *  rmd160 = Digest::RMD160.new               # =>#<Digest::RMD160>
37
 *  rmd160.update "ab"
38
 *  rmd160 << "c"                           # alias for #update
39
 *  rmd160.hexdigest                        # => "8eb208f7..."
40
 *
41
 *  # Use the same object to compute another digest
42
 *  rmd160.reset
43
 *  rmd160 << "message"
44
 *  rmd160.hexdigest                        # => "1dddbe1b..."
25 45
 */
26 46
void
27 47
Init_rmd160(void)
ext/digest/sha1/sha1init.c
19 19
};
20 20

  
21 21
/*
22
 * Document-class: Digest::SHA1 < Digest::Base
22 23
 * A class for calculating message digests using the SHA-1 Secure Hash
23 24
 * Algorithm by NIST (the US' National Institute of Standards and
24 25
 * Technology), described in FIPS PUB 180-1.
26
 *
27
 * See Digest::Instance for digest API.
28
 *
29
 * SHA-1 calculates a digest of 160 bits (20 bytes).
30
 *
31
 * == Examples
32
 *  require 'digest'
33
 *  
34
 *  # Compute a complete digest
35
 *  Digest::SHA1.hexdigest 'abc'      #=> "a9993e36..."
36
 *
37
 *  # Compute digest by chunks
38
 *  sha1 = Digest::SHA1.new               # =>#<Digest::SHA1>
39
 *  sha1.update "ab"
40
 *  sha1 << "c"                           # alias for #update
41
 *  sha1.hexdigest                        # => "a9993e36..."
42
 *
43
 *  # Use the same object to compute another digest
44
 *  sha1.reset
45
 *  sha1 << "message"
46
 *  sha1.hexdigest                        # => "6f9b9af3..."
25 47
 */
26 48
void
27 49
Init_sha1(void)
ext/digest/sha2/lib/sha2.rb
16 16
  #
17 17
  # A meta digest provider class for SHA256, SHA384 and SHA512.
18 18
  #
19
  # FIPS 180-2 describes SHA2 family of digest algorithms. It defines
20
  # three algorithms:
21
  # * one which works on chunks of 512 bits and returns a 256-bit
22
  #   digest (SHA256),
23
  # * one which works on chunks of 1024 bits and returns a 384-bit
24
  #   digest (SHA384),
25
  # * and one which works on chunks of 1024 bits and returns a 512-bit
26
  #   digest (SHA512).
27
  #
28
  # ==Examples
29
  #  require 'digest'
30
  #  
31
  #  # Compute a complete digest
32
  #  Digest::SHA2.hexdigest 'abc'          # => "ba7816bf8..."
33
  #  Digest::SHA2.new(256).hexdigest 'abc' # => "ba7816bf8..."
34
  #  Digest::SHA256.hexdigest 'abc'        # => "ba7816bf8..."
35
  #  
36
  #  Digest::SHA2.new(384).hexdigest 'abc' # => "cb00753f4..."
37
  #  Digest::SHA384.hexdigest 'abc'        # => "cb00753f4..."
38
  #  
39
  #  Digest::SHA2.new(512).hexdigest 'abc' # => "ddaf35a19..."
40
  #  Digest::SHA512.hexdigest 'abc'        # => "ddaf35a19..."
41
  #  
42
  #  # Compute digest by chunks
43
  #  sha2 = Digest::SHA2.new               # =>#<Digest::SHA2:256>
44
  #  sha2.update "ab"
45
  #  sha2 << "c"                           # alias for #update
46
  #  sha2.hexdigest                        # => "ba7816bf8..."
47
  #
48
  #  # Use the same object to compute another digest
49
  #  sha2.reset
50
  #  sha2 << "message"
51
  #  sha2.hexdigest                        # => "ab530a13e..."
52
  #
19 53
  class SHA2 < Digest::Class
20 54
    # call-seq:
21 55
    #   Digest::SHA2.new(bitlen = 256) -> digest_obj
22 56
    #
23
    # Creates a new SHA2 hash object with a given bit length.
57
    # Create a new SHA2 hash object with a given bit length.
24 58
    #
25 59
    # Valid bit lengths are 256, 384 and 512.
26 60
    def initialize(bitlen = 256)
......
40 74
    # call-seq:
41 75
    #   digest_obj.reset -> digest_obj
42 76
    #
43
    # Resets the digest to the initial state and returns self.
77
    # Reset the digest to the initial state and return self.
44 78
    def reset
45 79
      @sha2.reset
46 80
      self
......
50 84
    #   digest_obj.update(string) -> digest_obj
51 85
    #   digest_obj << string -> digest_obj
52 86
    #
53
    # Updates the digest using a given _string_ and returns self.
87
    # Update the digest using a given _string_ and return self.
54 88
    def update(str)
55 89
      @sha2.update(str)
56 90
      self
......
66 100
    # call-seq:
67 101
    #   digest_obj.block_length -> Integer
68 102
    #
69
    # Returns the block length of the digest in bytes.
103
    # Return the block length of the digest in bytes.
70 104
    #
71 105
    #   Digest::SHA256.new.block_length * 8
72 106
    #   # => 512
......
81 115
    # call-seq:
82 116
    #   digest_obj.digest_length -> Integer
83 117
    #
84
    # Returns the length of the hash value of the digest in bytes.
118
    # Return the length of the hash value (the digest) in bytes.
85 119
    #
86 120
    #   Digest::SHA256.new.digest_length * 8
87 121
    #   # => 256
88
-