From 0bdf887afc78fb049295d78a9018701da094b8c3 Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> Date: Wed, 25 Oct 2006 08:54:05 +0000 Subject: This commit was manufactured by cvs2svn to create branch 'ruby_1_8'. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@11222 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/digest/bubblebabble/.cvsignore | 4 + ext/digest/bubblebabble/bubblebabble.c | 142 +++++++++++++++++++++++++++++++++ ext/digest/bubblebabble/depend | 3 + ext/digest/bubblebabble/extconf.rb | 6 ++ ext/digest/sha2/lib/digest/sha2.rb | 73 +++++++++++++++++ 5 files changed, 228 insertions(+) create mode 100644 ext/digest/bubblebabble/.cvsignore create mode 100644 ext/digest/bubblebabble/bubblebabble.c create mode 100644 ext/digest/bubblebabble/depend create mode 100644 ext/digest/bubblebabble/extconf.rb create mode 100644 ext/digest/sha2/lib/digest/sha2.rb (limited to 'ext/digest') diff --git a/ext/digest/bubblebabble/.cvsignore b/ext/digest/bubblebabble/.cvsignore new file mode 100644 index 0000000000..814345ece8 --- /dev/null +++ b/ext/digest/bubblebabble/.cvsignore @@ -0,0 +1,4 @@ +Makefile +mkmf.log +*.def +extconf.h diff --git a/ext/digest/bubblebabble/bubblebabble.c b/ext/digest/bubblebabble/bubblebabble.c new file mode 100644 index 0000000000..3a03ceced0 --- /dev/null +++ b/ext/digest/bubblebabble/bubblebabble.c @@ -0,0 +1,142 @@ +/************************************************ + + bubblebabble.c - BubbleBabble encoding support + + $Author$ + created at: Fri Oct 13 18:31:42 JST 2006 + + Copyright (C) 2006 Akinori MUSHA + + $Id$ + +************************************************/ + +#include "ruby.h" +#include "digest.h" + +static ID id_digest; + +static VALUE +bubblebabble_str_new(VALUE str_digest) +{ + char *digest; + size_t digest_len; + VALUE str; + char *p; + int i, j, seed = 1; + static const char vowels[] = { + 'a', 'e', 'i', 'o', 'u', 'y' + }; + static const char consonants[] = { + 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', 'n', + 'p', 'r', 's', 't', 'v', 'z', 'x' + }; + + StringValue(str_digest); + digest = RSTRING_PTR(str_digest); + digest_len = RSTRING_LEN(str_digest); + + if ((LONG_MAX - 2) / 3 < (digest_len | 1)) { + rb_raise(rb_eRuntimeError, "digest string too long"); + } + + str = rb_str_new(0, (digest_len | 1) * 3 + 2); + p = RSTRING_PTR(str); + + i = j = 0; + p[j++] = 'x'; + + for (;;) { + unsigned char byte1, byte2; + + if (i >= digest_len) { + p[j++] = vowels[seed % 6]; + p[j++] = consonants[16]; + p[j++] = vowels[seed / 6]; + break; + } + + byte1 = digest[i++]; + p[j++] = vowels[(((byte1 >> 6) & 3) + seed) % 6]; + p[j++] = consonants[(byte1 >> 2) & 15]; + p[j++] = vowels[((byte1 & 3) + (seed / 6)) % 6]; + + if (i >= digest_len) { + break; + } + + byte2 = digest[i++]; + p[j++] = consonants[(byte2 >> 4) & 15]; + p[j++] = '-'; + p[j++] = consonants[byte2 & 15]; + + seed = (seed * 5 + byte1 * 7 + byte2) % 36; + } + + p[j] = 'x'; + + return str; +} + +/* + * call-seq: + * Digest.bubblebabble(string) -> bubblebabble_string + * + * Returns a BubbleBabble encoded version of a given _string_. + */ +static VALUE +rb_digest_s_bubblebabble(VALUE klass, VALUE str) +{ + return bubblebabble_str_new(str); +} + +/* + * call-seq: + * Digest::Class.bubblebabble(string, ...) -> hash_string + * + * Returns the BubbleBabble encoded hash value of a given _string_. + */ +static VALUE +rb_digest_class_s_bubblebabble(int argc, VALUE *argv, VALUE klass) +{ + return bubblebabble_str_new(rb_funcall2(klass, id_digest, argc, argv)); +} + +/* + * call-seq: + * digest_obj.bubblebabble -> hash_string + * + * Returns the resulting hash value in a Bubblebabble encoded form. + */ +static VALUE +rb_digest_instance_bubblebabble(VALUE self) +{ + return bubblebabble_str_new(rb_funcall(self, id_digest, 0)); +} + +/* + * This module adds some methods to Digest classes to perform + * BubbleBabble encoding. + */ +void +Init_bubblebabble(void) +{ + VALUE mDigest, mDigest_Instance, cDigest_Class; + + rb_require("digest"); + + mDigest = rb_path2class("Digest"); + mDigest_Instance = rb_path2class("Digest::Instance"); + cDigest_Class = rb_path2class("Digest::Class"); + + /* Digest::bubblebabble() */ + rb_define_module_function(mDigest, "bubblebabble", rb_digest_s_bubblebabble, 1); + + /* Digest::Class::bubblebabble() */ + rb_define_singleton_method(cDigest_Class, "bubblebabble", rb_digest_class_s_bubblebabble, -1); + + /* Digest::Instance#bubblebabble() */ + rb_define_method(mDigest_Instance, "bubblebabble", rb_digest_instance_bubblebabble, 0); + + id_digest = rb_intern("digest"); +} diff --git a/ext/digest/bubblebabble/depend b/ext/digest/bubblebabble/depend new file mode 100644 index 0000000000..b20148ded4 --- /dev/null +++ b/ext/digest/bubblebabble/depend @@ -0,0 +1,3 @@ +bubblebabble.o: bubblebabble.c $(srcdir)/../digest.h $(hdrdir)/ruby.h \ + $(topdir)/config.h $(hdrdir)/defines.h $(hdrdir)/intern.h \ + $(srcdir)/../defs.h diff --git a/ext/digest/bubblebabble/extconf.rb b/ext/digest/bubblebabble/extconf.rb new file mode 100644 index 0000000000..53cb83934a --- /dev/null +++ b/ext/digest/bubblebabble/extconf.rb @@ -0,0 +1,6 @@ +require 'mkmf' + +$defs << "-DHAVE_CONFIG_H" +$INCFLAGS << " -I$(srcdir)/.." + +create_makefile('digest/bubblebabble') diff --git a/ext/digest/sha2/lib/digest/sha2.rb b/ext/digest/sha2/lib/digest/sha2.rb new file mode 100644 index 0000000000..52dd639f9b --- /dev/null +++ b/ext/digest/sha2/lib/digest/sha2.rb @@ -0,0 +1,73 @@ +#-- +# sha2.rb - defines Digest::SHA2 class which wraps up the SHA256, +# SHA384, and SHA512 classes. +#++ +# Copyright (c) 2006 Akinori MUSHA +# +# All rights reserved. You can redistribute and/or modify it under the same +# terms as Ruby. +# +# $Id$ + +require 'digest' + +module Digest + # + # A meta digest provider class for SHA256, SHA384 and SHA512. + # + class SHA2 < Digest::Class + # call-seq: + # Digest::SHA2.new(bitlen = 256) -> digest_obj + # + # Creates a new SHA2 hash object with a given bit length. + def initialize(bitlen = 256) + case bitlen + when 256 + @sha2 = Digest::SHA256.new + when 384 + @sha2 = Digest::SHA384.new + when 512 + @sha2 = Digest::SHA512.new + else + raise ArgumentError, "unsupported bit length: %s" % bitlen.inspect + end + @bitlen = bitlen + end + + # :nodoc: + def reset + @sha2.reset + self + end + + # :nodoc: + def update(str) + @sha2.update(str) + self + end + alias << update + + def finish + @sha2.digest! + end + private :finish + + def block_length + @sha2.block_length + end + + def digest_length + @sha2.digest_length + end + + # :nodoc: + def initialize_copy(other) + @sha2 = other.instance_eval { @sha2.clone } + end + + # :nodoc: + def inspect + "#<%s:%d %s>" % [self.class.name, @bitlen, hexdigest] + end + end +end -- cgit v1.2.3