Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SHA-3 hash functions #271

Closed
wants to merge 6 commits into from
Closed

Support SHA-3 hash functions #271

wants to merge 6 commits into from

Conversation

yuan-xy
Copy link

@yuan-xy yuan-xy commented Aug 31, 2019

add SHA3_224, SHA3_256, SHA3_384 and SHA3_512 class under OpenSSL::Digest module.

@ioquatix
Copy link
Member

ioquatix commented Sep 2, 2019

I wonder if we can use something better than class name. e.g. constant.

@ioquatix
Copy link
Member

ioquatix commented Sep 2, 2019

Maybe have SHA3_224::NAME = "SHA3-224". What do you think?

@yuan-xy
Copy link
Author

yuan-xy commented Sep 2, 2019

Maybe have SHA3_224::NAME = "SHA3-224". What do you think?

Maybe it works, but I can't figure out how to implements this. In addtion, there is a 'name' method there:
rb_define_method(cDigest, "name", ossl_digest_name, 0);

@ioquatix
Copy link
Member

ioquatix commented Sep 2, 2019

@yuan-xy
Copy link
Author

yuan-xy commented Sep 3, 2019

how about this,

if(strncmp(name, "SHA3_", 5) && strlen(name)==9) {
char n_name[9];
strncpy(n_name, name, 9);
n_name[4] = '-';
md = EVP_get_digestbyname(n_name);

which is better than original code

char *pos = strchr(name, '_');
if(pos) {
	char n_name[9];
	char *n_pos;
	strncpy(n_name, name, 9);
	n_pos = strchr(n_name, '_');
	*n_pos = '-';
	md = EVP_get_digestbyname(n_name);

@yuan-xy
Copy link
Author

yuan-xy commented Sep 3, 2019

how about this code:

if(strcmp(name, "SHA3_224") {
md = EVP_get_digestbyname("SHA3-224");
else if(strcmp(name, "SHA3_256") {
md = EVP_get_digestbyname("SHA3-256");
else if(strcmp(name, "SHA3_384") {
md = EVP_get_digestbyname("SHA3-384");
else if(strcmp(name, "SHA3_512") {
md = EVP_get_digestbyname("SHA3-512");
}

@ioquatix
Copy link
Member

ioquatix commented Sep 4, 2019

I still think using constant is better, @mame what do you think?

@yuan-xy
Copy link
Author

yuan-xy commented Sep 5, 2019

Maybe someone else can help support SHA-3 hash functions in ruby.

@ioquatix
Copy link
Member

ioquatix commented Sep 5, 2019

So, for each class, you need to write something like:

rb_define_const(digest_class, "NAME", "SHA3-384");

Then, in the method to create it, you need to use:

rb_const_get_at(digest_class, rb_intern("NAME"));

Does that make sense?

@ioquatix
Copy link
Member

@yuan-xy you are very close to having a workable solution. Let me know if you'd like to finish this off.

@yuan-xy
Copy link
Author

yuan-xy commented Sep 28, 2019 via email

@yuan-xy
Copy link
Author

yuan-xy commented Oct 2, 2019

After many attempts, I still don't know how to implement it use rb_define_const.
The digest class has not initialized when ossl_evp_get_digestbyname being called. How to define this:

 rb_define_const(digest_class, "NAME", "SHA3-384");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants