-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Added Digest::SHA256 #2815
Added Digest::SHA256 #2815
Conversation
"0123456701234567012345670123456701234567012345670123456701234567", | ||
"8182cadb21af0e37c06414ece08e19c65bdb22c396d48ba7341012eea9ffdfdd", | ||
"gYLK2yGvDjfAZBTs4I4ZxlvbIsOW1IunNBAS7qn/390=", | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are those official test vectors? Or are they made up?
Do you have tests for known vulnerabilities in implementations, if there are any?
Thanks! I don't have means for a proper review (I'm on a phone). I believe SHA256 and SHA512 are actually the same thing (SHA2) but using different lengths. I'll read more about that and about SHA1, and see whether it could make sense to have a single SHA2 implementation, possibly sharing some code with SHA1. How fast is this implementation, compared to using OpenSSL::Digest for example? |
For the test vectors, I took the tests from As for the speed, I don't know. This implementation is based on the existing |
Yes, our benchmark stdlib has roughly the same API as Ruby's and that of the |
Ok, I did this small benchmark : require "benchmark"
require "openssl"
require "digest/sha256"
n = 10000
strings = [] of String
ssl_sha256 = OpenSSL::Digest.new("sha256")
n.times do
len = (rand * 100).to_i * 1024 # (1k..100k range)
str = "a" * len
strings << str
end
Benchmark.bm do |x|
x.report("OpenSSL::Digest") do
strings.each do |str|
ssl_sha256.reset
ssl_sha256 << str
ssl_sha256.digest
end
end
x.report("Digest::SHA256") do
strings.each {|str| Digest::SHA256.digest(str) }
end
end It generates strings full of 'a's, with a random length between 1k and 100k. |
The interface is the same as Digest::SHA1 A lot of code is duplicated between the two, maybe we should refactor into a Digest::Utils module? Or Digest::Utils::SHA for SHA-specific utils? The padding function, for example, is the same between all SHAs. Also, the Digest::* interface is probably gonna be implemented in the same way for every hash function, only the digest method will change. Maybe we should have an interface with hexdigest and base64digest implemented (as a base class or mixin, I don't know if it's a thing in crystal)? Tests are also provided, based on the test suite for SHA1.
Thanks for digging more. I'll definitely take a closer look. |
Any news about this ? |
I originally implemented |
The interface is the same as Digest::SHA1.
A lot of code is duplicated between the two, maybe we should refactor into a Digest::Utils module?
Or Digest::Utils::SHA for SHA-specific utils?
The padding function, for example, is the same between all SHAs.
Also, the Digest::* interface is probably gonna be implemented in the same way for every hash function, only the digest method will change.
Maybe we should have an interface with hexdigest and base64digest implemented (as a base class or mixin, I don't know if it's a thing in crystal)?
Tests are also provided, based on the test suite for SHA1.