Rename gem

This commit is contained in:
Alex Kotov 2019-06-08 16:53:04 +05:00
parent cd172e45d2
commit e7e8940b33
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
8 changed files with 50 additions and 50 deletions

View file

@ -18,18 +18,18 @@ and variable length. Variable length is not supported by this Ruby extension.
Unless the user specifies otherwise, this Ruby extension assumes 512-bit. Unless the user specifies otherwise, this Ruby extension assumes 512-bit.
```ruby ```ruby
require 'digest/sha3' require 'digest/keccak'
# Generate 512-bit digest. # Generate 512-bit digest.
Digest::SHA3.digest("foo") # => "\025\227\204*..." Digest::Keccak.digest("foo") # => "\025\227\204*..."
Digest::SHA3.hexdigest("foo") # => "1597842a..." Digest::Keccak.hexdigest("foo") # => "1597842a..."
# Generate 224-bit digest. # Generate 224-bit digest.
Digest::SHA3.digest("foo", 224) # => "\332\251M\247..." Digest::Keccak.digest("foo", 224) # => "\332\251M\247..."
Digest::SHA3.hexdigest("foo", 224) # => "daa94da7..." Digest::Keccak.hexdigest("foo", 224) # => "daa94da7..."
# Use this interface to feed data in chunks. 512-bit by default. # Use this interface to feed data in chunks. 512-bit by default.
digest = Digest::SHA3.new digest = Digest::Keccak.new
digest.update("f") digest.update("f")
digest.update("o") digest.update("o")
digest.update("o") digest.update("o")
@ -37,7 +37,7 @@ Unless the user specifies otherwise, this Ruby extension assumes 512-bit.
digest.hexdigest # => "1597842a..." digest.hexdigest # => "1597842a..."
# You can pass a hash length to the constructor. # You can pass a hash length to the constructor.
digest = Digest::SHA3.new(224) digest = Digest::Keccak.new(224)
``` ```

View file

@ -3,11 +3,11 @@
lib = File.expand_path('lib', __dir__).freeze lib = File.expand_path('lib', __dir__).freeze
$LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib $LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
require 'digest/sha3/version' require 'digest/keccak/version'
Gem::Specification.new do |spec| Gem::Specification.new do |spec|
spec.name = 'digest-sha3' spec.name = 'digest-keccak'
spec.version = Digest::SHA3::Version::STRING spec.version = Digest::Keccak::Version::STRING
spec.license = 'MIT' spec.license = 'MIT'
spec.homepage = 'https://github.com/kotovalexarian/digest-keccak.rb' spec.homepage = 'https://github.com/kotovalexarian/digest-keccak.rb'
spec.summary = 'The Keccak cryptographic hash function.' spec.summary = 'The Keccak cryptographic hash function.'
@ -35,7 +35,7 @@ Gem::Specification.new do |spec|
'README.md', 'README.md',
'LICENSE', 'LICENSE',
'Makefile', 'Makefile',
'digest-sha3.gemspec', 'digest-keccak.gemspec',
'ext/**/*.{c,h,rb}', 'ext/**/*.{c,h,rb}',
'lib/**/*', 'lib/**/*',
] ]

View file

@ -4,4 +4,4 @@ have_header('ruby/digest.h')
have_func('rb_str_set_len') have_func('rb_str_set_len')
$CFLAGS << " -fvisibility=hidden" $CFLAGS << " -fvisibility=hidden"
create_makefile('digest/sha3') create_makefile('digest/keccak')

View file

@ -9,23 +9,23 @@
#define MAX_DIGEST_SIZE 64 #define MAX_DIGEST_SIZE 64
#define DEFAULT_DIGEST_LEN 512 #define DEFAULT_DIGEST_LEN 512
static int sha3_init_func(hashState *ctx); static int keccak_init_func(hashState *ctx);
static void sha3_update_func(hashState *ctx, unsigned char *str, size_t len); static void keccak_update_func(hashState *ctx, unsigned char *str, size_t len)keccak
static int sha3_finish_func(hashState *ctx, unsigned char *digest); static int keccak_finish_func(hashState *ctx, unsigned char *digest);
/* /*
Metadata definition for the SHA3 algorithm. Metadata definition for the Keccak algorithm.
Defines the Version, sizes for block and digest as well as Defines the Version, sizes for block and digest as well as
the entry points for the algorithms the entry points for the algorithms
*/ */
static rb_digest_metadata_t sha3 = { static rb_digest_metadata_t keccak = {
RUBY_DIGEST_API_VERSION, RUBY_DIGEST_API_VERSION,
DEFAULT_DIGEST_LEN, DEFAULT_DIGEST_LEN,
KeccakPermutationSize - (2 * DEFAULT_DIGEST_LEN), //size of blocks KeccakPermutationSize - (2 * DEFAULT_DIGEST_LEN), //size of blocks
sizeof(hashState), //size of context for the object we'll be passed in below functions. sizeof(hashState), //size of context for the object we'll be passed in below functions.
(rb_digest_hash_init_func_t)sha3_init_func, (rb_digest_hash_init_func_t)keccak_init_func,
(rb_digest_hash_update_func_t)sha3_update_func, (rb_digest_hash_update_func_t)keccak_update_func,
(rb_digest_hash_finish_func_t)sha3_finish_func, (rb_digest_hash_finish_func_t)keccak_finish_func,
}; };
/* Initialization function for the algorithm, /* Initialization function for the algorithm,
@ -33,7 +33,7 @@ static rb_digest_metadata_t sha3 = {
we override initialize to do custom hash size, so we don't care too much here. we override initialize to do custom hash size, so we don't care too much here.
*/ */
static int static int
sha3_init_func(hashState *ctx) { keccak_init_func(hashState *ctx) {
// Just return a 1 ' successful' we override the init function // Just return a 1 ' successful' we override the init function
// so this is not necessary // so this is not necessary
// the base class alloc calls this to initialize the algorithm // the base class alloc calls this to initialize the algorithm
@ -42,29 +42,29 @@ sha3_init_func(hashState *ctx) {
/* Update function, take the current context and add str to it */ /* Update function, take the current context and add str to it */
static void static void
sha3_update_func(hashState *ctx, unsigned char *str, size_t len) { keccak_update_func(hashState *ctx, unsigned char *str, size_t len) {
Update(ctx, str, len * 8); Update(ctx, str, len * 8);
} }
/* Finish the hash calculation and return the finished string */ /* Finish the hash calculation and return the finished string */
static int static int
sha3_finish_func(hashState *ctx, unsigned char *digest) { keccak_finish_func(hashState *ctx, unsigned char *digest) {
Final(ctx, digest); Final(ctx, digest);
return 1; return 1;
} }
/* Ruby method. Digest::SHA3#finish() /* Ruby method. Digest::Keccak#finish()
* No Arguments * No Arguments
* @returns [String] Encoded Digest String * @returns [String] Encoded Digest String
*/ */
static VALUE static VALUE
rb_sha3_finish(VALUE self) { rb_keccak_finish(VALUE self) {
hashState *ctx; hashState *ctx;
VALUE digest; VALUE digest;
ctx = (hashState *)RTYPEDDATA_DATA(self); ctx = (hashState *)RTYPEDDATA_DATA(self);
digest = rb_str_new(0, ctx->capacity / 2 / 8); digest = rb_str_new(0, ctx->capacity / 2 / 8);
sha3_finish_func(ctx, (unsigned char *)RSTRING_PTR(digest)); keccak_finish_func(ctx, (unsigned char *)RSTRING_PTR(digest));
return digest; return digest;
} }
@ -73,7 +73,7 @@ rb_sha3_finish(VALUE self) {
* initialize the ctx with the bitlength * initialize the ctx with the bitlength
*/ */
static void static void
sha3_init(hashState *ctx, size_t bitlen) { keccak_init(hashState *ctx, size_t bitlen) {
switch (Init(ctx, bitlen)) { switch (Init(ctx, bitlen)) {
case SUCCESS: case SUCCESS:
return; return;
@ -86,12 +86,12 @@ sha3_init(hashState *ctx, size_t bitlen) {
} }
} }
/* Ruby method. Digest::SHA3.new(hashlen) /* Ruby method. Digest::Keccak.new(hashlen)
* @param hashlen The length of hash, only supports 224, 256, 384 or 512 * @param hashlen The length of hash, only supports 224, 256, 384 or 512
* @returns [Digest::SHA3] new object. * @returns [Digest::Keccak] new object.
*/ */
static VALUE static VALUE
rb_sha3_initialize(int argc, VALUE *argv, VALUE self) { rb_keccak_initialize(int argc, VALUE *argv, VALUE self) {
hashState *ctx; hashState *ctx;
VALUE hashlen; VALUE hashlen;
int i_hashlen; int i_hashlen;
@ -106,27 +106,27 @@ rb_sha3_initialize(int argc, VALUE *argv, VALUE self) {
} }
ctx = (hashState *)RTYPEDDATA_DATA(self); ctx = (hashState *)RTYPEDDATA_DATA(self);
sha3_init(ctx, i_hashlen); keccak_init(ctx, i_hashlen);
return rb_call_super(0, NULL); return rb_call_super(0, NULL);
} }
/* Ruby method. Digest::SHA3#digest_length /* Ruby method. Digest::Keccak#digest_length
* @returns [Numeric] Length of the digest. * @returns [Numeric] Length of the digest.
*/ */
static VALUE static VALUE
rb_sha3_digest_length(VALUE self) { rb_keccak_digest_length(VALUE self) {
hashState *ctx; hashState *ctx;
ctx = (hashState *)RTYPEDDATA_DATA(self); ctx = (hashState *)RTYPEDDATA_DATA(self);
return INT2FIX(ctx->capacity / 2 / 8); return INT2FIX(ctx->capacity / 2 / 8);
} }
/* Ruby method. Digest::SHA3#block_length /* Ruby method. Digest::Keccak#block_length
* @returns [Numeric] Length of blocks in this digest. * @returns [Numeric] Length of blocks in this digest.
*/ */
static VALUE static VALUE
rb_sha3_block_length(VALUE self) { rb_keccak_block_length(VALUE self) {
hashState *ctx; hashState *ctx;
ctx = (hashState *)RTYPEDDATA_DATA(self); ctx = (hashState *)RTYPEDDATA_DATA(self);
@ -134,20 +134,20 @@ rb_sha3_block_length(VALUE self) {
} }
void __attribute__((visibility("default"))) void __attribute__((visibility("default")))
Init_sha3() { Init_keccak() {
VALUE mDigest, cDigest_Base, cSHA3; VALUE mDigest, cDigest_Base, cKeccak;
rb_require("digest"); rb_require("digest");
mDigest = rb_path2class("Digest"); mDigest = rb_path2class("Digest");
cDigest_Base = rb_path2class("Digest::Base"); cDigest_Base = rb_path2class("Digest::Base");
cSHA3 = rb_define_class_under(mDigest, "SHA3", cDigest_Base); cKeccak = rb_define_class_under(mDigest, "Keccak", cDigest_Base);
rb_iv_set(cSHA3, "metadata", Data_Wrap_Struct(0, 0, 0, (void *)&sha3)); rb_iv_set(cKeccak, "metadata", Data_Wrap_Struct(0, 0, 0, (void *)&keccak));
rb_define_method(cSHA3, "initialize", rb_sha3_initialize, -1); rb_define_method(cKeccak, "initialize", rb_keccak_initialize, -1);
rb_define_method(cSHA3, "digest_length", rb_sha3_digest_length, 0); rb_define_method(cKeccak, "digest_length", rb_keccak_digest_length, 0);
rb_define_method(cSHA3, "block_length", rb_sha3_block_length, 0); rb_define_method(cKeccak, "block_length", rb_keccak_block_length, 0);
rb_define_method(cSHA3, "finish", rb_sha3_finish, 0); rb_define_method(cKeccak, "finish", rb_keccak_finish, 0);
} }

View file

@ -1,5 +1,5 @@
module Digest module Digest
class SHA3 class Keccak
module Version module Version
STRING = "1.1.0" STRING = "1.1.0"
end end

View file

@ -18,7 +18,7 @@ def generate
require 'test/unit' require 'test/unit'
class SHA3Tests < Test::Unit::TestCase class KeccakTests < Test::Unit::TestCase
} }
FILES.each do |path, hashlen| FILES.each do |path, hashlen|
@ -33,7 +33,7 @@ def generate
name = File.basename(path).split('.')[0] name = File.basename(path).split('.')[0]
puts %Q{ puts %Q{
def test_#{name}_#{length} def test_#{name}_#{length}
inst = Digest::SHA3.new(#{hashlen}) inst = Digest::Keccak.new(#{hashlen})
inst.update(#{msg_raw.inspect}) inst.update(#{msg_raw.inspect})
assert_equal #{md.inspect}, inst.hexdigest assert_equal #{md.inspect}, inst.hexdigest
end end

View file

@ -1,5 +1,5 @@
$LOAD_PATH.unshift(File.expand_path("lib")) $LOAD_PATH.unshift(File.expand_path("lib"))
$LOAD_PATH.unshift(File.expand_path("ext")) $LOAD_PATH.unshift(File.expand_path("ext"))
require 'digest/sha3' require 'digest/keccak'
require File.expand_path('test/test_usage') require File.expand_path('test/test_usage')
require File.expand_path('test/test_vectors') require File.expand_path('test/test_vectors')

View file

@ -1,8 +1,8 @@
require 'test/unit' require 'test/unit'
class SHA3UsageTest < Test::Unit::TestCase class KeccakUsageTest < Test::Unit::TestCase
def init(hashsize = 512) def init(hashsize = 512)
@digest = Digest::SHA3.new(hashsize) @digest = Digest::Keccak.new(hashsize)
end end
def test_copy def test_copy
@ -16,7 +16,7 @@ class SHA3UsageTest < Test::Unit::TestCase
def test_class_methods def test_class_methods
assert_equal 'a9cab59eb40a10b246290f2d6086e32e3689faf1d26b470c899f2802', assert_equal 'a9cab59eb40a10b246290f2d6086e32e3689faf1d26b470c899f2802',
Digest::SHA3.hexdigest("\xcc", 224) Digest::Keccak.hexdigest("\xcc", 224)
end end
def test_update def test_update