-
-
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
Add OpenSSL::DigestIO to wrap an IO while calculating a digest #4260
Conversation
src/openssl/digest/digest_io.cr
Outdated
def read(slice : Bytes) | ||
read_bytes = io.read(slice) | ||
if @digest_on_read | ||
digest_algorithm.update(Bytes.new(slice.to_unsafe, read_bytes, read_only: true)) |
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.
The usual way to do this is slice[0, read_bytes]
instead of using Bytes.new
and to_unsafe
.
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.
Good point. Fixed.
src/openssl/digest/digest_io.cr
Outdated
def initialize(@io : IO, @digest_algorithm : OpenSSL::Digest, *, @digest_on_read = true, @digest_on_write = true) | ||
end | ||
|
||
def initialize(@io : IO, algorithm : String, *, @digest_on_read = true, @digest_on_write = true) |
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.
I'm not sure I see a usecase for hashing read
and write
to the same hash, and it could cause some confusion. Maybe it would be best to make this exclusive (read or write digest).
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.
Neither do I. I've changed both flags in favour of an enum Read/Write.
src/openssl/digest/digest_io.cr
Outdated
getter digest_on_write : Bool | ||
|
||
delegate close, closed?, flush, peek, tty?, rewind, to: @io | ||
delegate digest, to: @digest_algorithm |
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.
What about hexdigest
and base64digest
?
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.
Added
01d70ea
to
75d7195
Compare
src/openssl/digest/digest_io.cr
Outdated
@@ -0,0 +1,55 @@ | |||
require "./digest_base" | |||
|
|||
# Wraps an IO by calculating a specified digest on read and/or write operations |
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.
Docs mention and
, whereas right now it's either read or write.
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.
It's amazing how fast docs can become outdated. Thanks for the attention to detail!
75d7195
to
39e571d
Compare
src/openssl/digest/digest_io.cr
Outdated
delegate digest, hexdigest, base64digest, to: @digest_algorithm | ||
|
||
def initialize(@io : IO, @digest_algorithm : OpenSSL::Digest, *, @digest_on_read = true, @digest_on_write = true) | ||
enum DigestMode |
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.
Maybe rename this as just Mode
as the surrounding class gives context. Maybe also bring out the members to the class level, so you only have to reference OpenSSL::DigestIO::Read
.
src/openssl/digest/digest_io.cr
Outdated
delegate digest, hexdigest, base64digest, to: @digest_algorithm | ||
|
||
enum DigestMode | ||
Read, |
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.
Why a comma here? (I think the formatter should remove this)
Also, maybe fix the typo ( |
39e571d
to
5bda0ef
Compare
@RX14 done and done! |
src/openssl/digest/digest_io.cr
Outdated
@@ -0,0 +1,55 @@ | |||
require "./digest_base" | |||
|
|||
# Wraps an IO by calculating a specified digest on read or write operations |
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.
Missing dot ending the sentence :)
io_deferred.cr
Outdated
@@ -0,0 +1,175 @@ | |||
module IO |
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.
Debug leftovers (along with the binary)?
foo.cr
Outdated
@@ -0,0 +1,5 @@ | |||
# puts "A" |
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.
Debug leftovers?
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.
Certainly. Fixing now.
5bda0ef
to
5658d21
Compare
This PR is going to be perfect after all these reviews haha |
src/openssl/digest/digest_io.cr
Outdated
# io.read(buffer) | ||
# io.digest # => 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae | ||
# ``` | ||
# |
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.
Redundant empty comment line :)
src/openssl/digest/digest_io.cr
Outdated
|
||
def read(slice : Bytes) | ||
read_bytes = io.read(slice) | ||
if @mode == DigestMode::Read |
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.
Why not use @mode.read?
Enum helper instead?
src/openssl/digest/digest_io.cr
Outdated
end | ||
|
||
def write(slice : Bytes) | ||
if @mode == DigestMode::Write |
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.
Why not use @mode.write?
Enum helper instead?
5658d21
to
944e148
Compare
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.
I think the only remaining piece here is the documentation block (and a rebase against latest master)
😄
src/openssl/digest/digest_io.cr
Outdated
@@ -0,0 +1,54 @@ | |||
require "./digest_base" | |||
|
|||
# Wraps an IO by calculating a specified digest on read or write operations. |
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.
Please move the documentation block to the DigestIO
class instead. This will avoid OpenSSL
module receive the digest-related documentation.
Awesome! Will do that next week. I've fried my Mac's HD and are setting everything back up :-P |
944e148
to
979d6c4
Compare
@luislavena done! Rebase can be done without conflicts as well. |
Thanks @mverzilli! |
Wraps an IO by calculating a specified digest on read and/or write operations
Example