-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Adding functionality for dirhash.
Signed-off-by: Matthias Glastra <[email protected]>
- Loading branch information
Showing
6 changed files
with
136 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cryptoutil | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
// DirHashSha256 is the "h1:" directory hash function, using SHA-256. | ||
// | ||
// DirHashSha256 returns a SHA-256 hash of a summary | ||
// prepared as if by the Unix command: | ||
// | ||
// sha256sum $(find . -type f | sort) | sha256sum | ||
// | ||
// More precisely, the hashed summary contains a single line for each file in the list, | ||
// ordered by sort.Strings applied to the file names, where each line consists of | ||
// the hexadecimal SHA-256 hash of the file content, | ||
// two spaces (U+0020), the file name, and a newline (U+000A). | ||
// | ||
// File names with newlines (U+000A) are disallowed. | ||
func DirhHashSha256(files []string, open func(string) (io.ReadCloser, error)) (string, error) { | ||
h := sha256.New() | ||
files = append([]string(nil), files...) | ||
sort.Strings(files) | ||
for _, file := range files { | ||
if strings.Contains(file, "\n") { | ||
return "", errors.New("dirhash: filenames with newlines are not supported") | ||
} | ||
r, err := open(file) | ||
if err != nil { | ||
return "", err | ||
} | ||
hf := sha256.New() | ||
_, err = io.Copy(hf, r) | ||
r.Close() | ||
if err != nil { | ||
return "", err | ||
} | ||
fmt.Fprintf(h, "%x %s\n", hf.Sum(nil), file) | ||
} | ||
return hex.EncodeToString(h.Sum(nil)), nil | ||
} |