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

How can I use sha3 with OpenSSL in ruby? #9

Closed
DaHoopster opened this issue Jan 24, 2019 · 7 comments
Closed

How can I use sha3 with OpenSSL in ruby? #9

DaHoopster opened this issue Jan 24, 2019 · 7 comments

Comments

@DaHoopster
Copy link

I am trying to use sha3 as the digest to sign a message with OpenSSL. But it looks like Ruby 2.5.1 doesn't work with this library. Any thoughts on how I can combine the two? Thanks!

@johanns
Copy link
Owner

johanns commented Jan 24, 2019

But it looks like Ruby 2.5.1 doesn't work with this library.

Is there an error message you're receiving when trying to use it? What happens if you run the following code from IRB or Pry (assuming your already installed the gem gem install sha3)?

require 'sha3'
SHA3::Digest::SHA224.new()

Please post any error message(s), and operating system information (sample code would be great too).

Any thoughts on how I can combine the two?

I'd love to help, but I'm unsure how you're trying to "combine" them. Please provide more context.

@DaHoopster
Copy link
Author

DaHoopster commented Jan 24, 2019

Sorry, pardon the ambiguities, here are the details 😄

I was attempting to do the below:

require 'sha3'
require 'openssl'

p_key = OpenSSL::PKey::EC.new('secp256k1').generate_key
p_key.sign(SHA3::Digest.new(:sha256), 'a_super_secret')

Because sha3 isn't a subclass of OpenSSL::Digest, I got the follow error:

TypeError:
       wrong argument type SHA3::Digest (expected OpenSSL/Digest)

And I am stuck ...

@johanns
Copy link
Owner

johanns commented Jan 25, 2019

That's helpful, but unfortunately I don't have a solution for your particular use case. I suppose it would be possible to change the base class to OpenSSL::Digest from Digest::Class (which is also the parent class for OpenSSL::Digest), but that doesn't like a good idea because the actual SHA3 implementation isn't part of OpenSSL library.

Assuming that you must use SHA3, you can either fork this project, and change the base class to OpenSSL::Digest (see below), or create a new OpenSSL::Digest subclass that proxies data to SHA3::Digest.

cSHA3Digest = rb_define_class_under(mSHA3, "Digest", rb_path2class("Digest::Class"));

@DaHoopster
Copy link
Author

Thanks @johanns , I will try forking the project

@DaHoopster
Copy link
Author

DaHoopster commented Jan 25, 2019

Hmm still no good. It looks like the error message comes from the C extension within ruby's openssl code. I read it on stackoverflow (lost the link) that in order to add a custom digest, a C struct needs to be used as the context, like this line: https://github.com/ruby/openssl/blob/master/ext/openssl/ossl_digest.h#L16

The type error comes from this line: https://github.com/ruby/openssl/blob/fdcda971a26895ea5c5015a90671ee73039d55e8/ext/openssl/ossl.h#L59

I am not good with C tho ...

@DaHoopster
Copy link
Author

Actually I made it work via pure Ruby by subclassing OpenSSL::Digest. The trick is also call super in the constructor. Thanks!

@katpadi
Copy link

katpadi commented Oct 3, 2019

Hi @DaHoopster,

How were you able to make it work? Did you actually monkeypatch the SHA3::Digest class? Can you point me to the right direction? Thanks.

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

No branches or pull requests

3 participants