-
-
Notifications
You must be signed in to change notification settings - Fork 339
feat: implement Hash
and HashSums
to performing hashing based on user provided hashers
#215
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8d29c1f
Add New Hash And HashSums Functions
mahadzaryab1 55ae818
Add Tests For New Methods
mahadzaryab1 4f310b1
Add New Examples
mahadzaryab1 24d61cf
Update README
mahadzaryab1 e509b3b
Point Old Implementation To New
mahadzaryab1 9a7aca8
Add Deprecation Note
mahadzaryab1 e8cb2aa
Address Feedback From PR Review
mahadzaryab1 14f87ca
Address Feedback From PR Review
mahadzaryab1 76323c3
Address Feedback From PR Review
mahadzaryab1 c26095f
Use FilterLine Instead of FilterScan
mahadzaryab1 169d6c1
Revert "Use FilterLine Instead of FilterScan"
mahadzaryab1 bb277b5
Add Test Case For Empty File That Can't Be Hashed
mahadzaryab1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |||||
"encoding/hex" | ||||||
"encoding/json" | ||||||
"fmt" | ||||||
"hash" | ||||||
"io" | ||||||
"math" | ||||||
"net/http" | ||||||
|
@@ -650,6 +651,38 @@ func (p *Pipe) Get(url string) *Pipe { | |||||
return p.Do(req) | ||||||
} | ||||||
|
||||||
// Hash returns the hex-encoded hash of the entire contents of the | ||||||
bitfield marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
// pipe based on the provided hasher, or an error. | ||||||
func (p *Pipe) Hash(hasher hash.Hash) (string, error) { | ||||||
if p.Error() != nil { | ||||||
return "", p.Error() | ||||||
} | ||||||
_, err := io.Copy(hasher, p) | ||||||
if err != nil { | ||||||
p.SetError(err) | ||||||
return "", err | ||||||
} | ||||||
return hex.EncodeToString(hasher.Sum(nil)), nil | ||||||
} | ||||||
|
||||||
// HashSums reads paths from the pipe, one per line, and produces the | ||||||
// hex-encoded hash of each corresponding file based on the provided hasher, | ||||||
// one per line. Any files that cannot be opened or read will be ignored. | ||||||
func (p *Pipe) HashSums(hasher hash.Hash) *Pipe { | ||||||
return p.FilterScan(func(line string, w io.Writer) { | ||||||
bitfield marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
f, err := os.Open(line) | ||||||
if err != nil { | ||||||
return // skip unopenable files | ||||||
} | ||||||
defer f.Close() | ||||||
_, err = io.Copy(hasher, f) | ||||||
if err != nil { | ||||||
return // skip unreadable files | ||||||
} | ||||||
fmt.Fprintln(w, hex.EncodeToString(hasher.Sum(nil))) | ||||||
}) | ||||||
} | ||||||
|
||||||
// Join joins all the lines in the pipe's contents into a single | ||||||
// space-separated string, which will always end with a newline. | ||||||
func (p *Pipe) Join() *Pipe { | ||||||
|
@@ -816,36 +849,19 @@ func (p *Pipe) SetError(err error) { | |||||
|
||||||
// SHA256Sum returns the hex-encoded SHA-256 hash of the entire contents of the | ||||||
// pipe, or an error. | ||||||
// Deprecated: SHA256Sums has been deprecated by Hash. To get the SHA 256 | ||||||
bitfield marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
// hash for the contents of the pipe, call `Hash(sha256.new())` | ||||||
func (p *Pipe) SHA256Sum() (string, error) { | ||||||
if p.Error() != nil { | ||||||
return "", p.Error() | ||||||
} | ||||||
hasher := sha256.New() | ||||||
_, err := io.Copy(hasher, p) | ||||||
if err != nil { | ||||||
p.SetError(err) | ||||||
return "", err | ||||||
} | ||||||
return hex.EncodeToString(hasher.Sum(nil)), p.Error() | ||||||
return p.Hash(sha256.New()) | ||||||
} | ||||||
|
||||||
// SHA256Sums reads paths from the pipe, one per line, and produces the | ||||||
// hex-encoded SHA-256 hash of each corresponding file, one per line. Any files | ||||||
// that cannot be opened or read will be ignored. | ||||||
// Deprecated: SHA256Sums has been deprecated by HashSums. To get the SHA 256 | ||||||
bitfield marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
// encoding for the paths in the pipe, call `HashSums(sha256.new())` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
func (p *Pipe) SHA256Sums() *Pipe { | ||||||
return p.FilterScan(func(line string, w io.Writer) { | ||||||
f, err := os.Open(line) | ||||||
if err != nil { | ||||||
return // skip unopenable files | ||||||
} | ||||||
defer f.Close() | ||||||
h := sha256.New() | ||||||
_, err = io.Copy(h, f) | ||||||
if err != nil { | ||||||
return // skip unreadable files | ||||||
} | ||||||
fmt.Fprintln(w, hex.EncodeToString(h.Sum(nil))) | ||||||
}) | ||||||
return p.HashSums(sha256.New()) | ||||||
} | ||||||
|
||||||
// Slice returns the pipe's contents as a slice of strings, one element per | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.