Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/db/migrations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ create table if not exists file_hashes (
sha3_256 text,
sha3_384 text,
sha3_512 text,
ed2k text,
size integer not null
);
4 changes: 2 additions & 2 deletions assets/db/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
-- name: FileHashInsertReplace :one
insert or replace into file_hashes (
filepath, crc32, xxhash64, md4, md5, sha1, sha256, sha512,
blake2b_256, blake2b_512, blake3, sha3_224, sha3_256, sha3_384, sha3_512,
blake2b_256, blake2b_512, blake3, sha3_224, sha3_256, sha3_384, sha3_512, ed2k,
size
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
returning *;

-- name: FileHashByFilePath :one
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1
github.com/spf13/cobra v1.8.1
github.com/zeebo/blake3 v0.2.3
go.felesatra.moe/hash/ed2k v1.0.2
golang.org/x/crypto v0.31.0
modernc.org/sqlite v1.40.1
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ github.com/zeebo/blake3 v0.2.3 h1:TFoLXsjeXqRNFxSbk35Dk4YtszE/MQQGK10BH4ptoTg=
github.com/zeebo/blake3 v0.2.3/go.mod h1:mjJjZpnsyIVtVgTOSpJ9vmRE4wgDeyt2HU3qXvvKCaQ=
github.com/zeebo/pcg v1.0.1 h1:lyqfGeWiv4ahac6ttHs+I5hwtH/+1mrhlCtVNQM2kHo=
github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4=
go.felesatra.moe/hash/ed2k v1.0.2 h1:veuWruebB5r8WCsQUyTbTFhVqlrGI58LlwH0G+JCBp0=
go.felesatra.moe/hash/ed2k v1.0.2/go.mod h1:iJcvQPdpma+fJfAHbrIFPjT6UYEN6GFsgkm+SB+QHdY=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
Expand Down
2 changes: 1 addition & 1 deletion processor/database/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion processor/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions processor/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions processor/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ func toSum(input chan Result) string {
if hasHash(HashNames.Sha3512) {
str.WriteString(res.Sha3512 + " " + res.File + "\n")
}
if hasHash(HashNames.Ed2k) {
str.WriteString(res.Ed2k + " " + res.File + "\n")
}

if !NoStream && FileOutput == "" {
fmt.Print(str.String())
Expand Down Expand Up @@ -187,6 +190,9 @@ func toHashOnly(input chan Result) (string, bool) {
if hasHash(HashNames.Sha3512) {
str.WriteString(res.Sha3512 + "\n")
}
if hasHash(HashNames.Ed2k) {
str.WriteString(res.Ed2k + "\n")
}

if !NoStream && FileOutput == "" {
fmt.Print(str.String())
Expand Down Expand Up @@ -253,6 +259,9 @@ func toText(input chan Result) (string, bool) {
if hasHash(HashNames.Sha3512) {
str.WriteString(" SHA3-512 " + res.Sha3512 + "\n")
}
if hasHash(HashNames.Ed2k) {
str.WriteString(" ed2k " + res.Ed2k + "\n")
}

if !NoStream && FileOutput == "" {
fmt.Print(str.String())
Expand Down Expand Up @@ -371,6 +380,7 @@ func toSqlite(input chan Result) (string, bool) {
Sha3256: toSqlNull(res.Sha3256),
Sha3384: toSqlNull(res.Sha3384),
Sha3512: toSqlNull(res.Sha3512),
Ed2k: toSqlNull(res.Ed2k),
Size: res.Bytes,
})
if err != nil {
Expand Down Expand Up @@ -427,6 +437,7 @@ func printHashes() {
fmt.Printf(" SHA3-256 (%s)\n", HashNames.Sha3256)
fmt.Printf(" SHA3-384 (%s)\n", HashNames.Sha3384)
fmt.Printf(" SHA3-512 (%s)\n", HashNames.Sha3512)
fmt.Printf(" ed2k (%s)\n", HashNames.Ed2k)
}

func contains(list []string, v string) bool {
Expand Down
5 changes: 3 additions & 2 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ var HashNames = Result{
Sha3256: "sha3256",
Sha3384: "sha3384",
Sha3512: "sha3512",
Ed2k: "ed2k",
}

// Process is the main entry point of the command line it sets everything up and starts running
Expand All @@ -107,13 +108,13 @@ func Process() {
}
}

// If nothing was supplied as an argument to run against assume run against everything in the
// If nothing was supplied as an argument to run against, assume run against everything in the
// current directory recursively
if len(DirFilePaths) == 0 {
DirFilePaths = append(DirFilePaths, ".")
}

// If a single argument is supplied enable recursive as if its a file no problem
// If a single argument is supplied, enable recursive as if it's a file no problem
// but if its a directory the user probably wants to hash everything in that directory
if len(DirFilePaths) == 1 {
Recursive = true
Expand Down
1 change: 1 addition & 0 deletions processor/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Result struct {
Sha3256 string `json:",omitempty"`
Sha3384 string `json:",omitempty"`
Sha3512 string `json:",omitempty"`
Ed2k string `json:"ed2k,omitempty"`
Bytes int64
MTime *time.Time `json:",omitzero"`
}
60 changes: 60 additions & 0 deletions processor/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/gosuri/uiprogress"
"github.com/minio/blake2b-simd"
"github.com/zeebo/blake3"
"go.felesatra.moe/hash/ed2k"
"golang.org/x/crypto/md4"
"golang.org/x/crypto/sha3"
)
Expand Down Expand Up @@ -166,6 +167,7 @@ func processScanner(filename string, fsize int, bar *uiprogress.Bar) (Result, er
sha3_256_d := sha3.New256()
sha3_384_d := sha3.New384()
sha3_512_d := sha3.New512()
ed2k_d := ed2k.New()

crc32c := make(chan []byte, 10)
xxhash64c := make(chan []byte, 10)
Expand All @@ -181,6 +183,7 @@ func processScanner(filename string, fsize int, bar *uiprogress.Bar) (Result, er
sha3_256_c := make(chan []byte, 10)
sha3_384_c := make(chan []byte, 10)
sha3_512_c := make(chan []byte, 10)
ed2k_c := make(chan []byte, 10)

var wg sync.WaitGroup

Expand Down Expand Up @@ -320,6 +323,16 @@ func processScanner(filename string, fsize int, bar *uiprogress.Bar) (Result, er
wg.Done()
}()
}
if hasHash(HashNames.Ed2k) {
wg.Add(1)
go func() {
for b := range ed2k_c {
// safe as it says it never returns an error
_, _ = ed2k_d.Write(b)
}
wg.Done()
}()
}

sum := 0
data := make([]byte, 4_194_304)
Expand Down Expand Up @@ -390,6 +403,9 @@ func processScanner(filename string, fsize int, bar *uiprogress.Bar) (Result, er
if hasHash(HashNames.Sha3512) {
sha3_512_c <- tmp[:n]
}
if hasHash(HashNames.Ed2k) {
ed2k_c <- tmp[:n]
}

if err == io.EOF {
break
Expand All @@ -410,6 +426,7 @@ func processScanner(filename string, fsize int, bar *uiprogress.Bar) (Result, er
close(sha3_256_c)
close(sha3_384_c)
close(sha3_512_c)
close(ed2k_c)

wg.Wait()

Expand All @@ -430,6 +447,7 @@ func processScanner(filename string, fsize int, bar *uiprogress.Bar) (Result, er
Sha3256: encodeIfHashEnabled(sha3_256_d, HashNames.Sha3256),
Sha3384: encodeIfHashEnabled(sha3_384_d, HashNames.Sha3384),
Sha3512: encodeIfHashEnabled(sha3_512_d, HashNames.Sha3512),
Ed2k: encodeIfHashEnabled(ed2k_d, HashNames.Ed2k),
}, nil
}

Expand All @@ -452,6 +470,7 @@ func processStandardInput(output chan Result) {
sha3_256_d := sha3.New256()
sha3_384_d := sha3.New384()
sha3_512_d := sha3.New512()
ed2k_d := ed2k.New()

crc32c := make(chan []byte, 10)
xxhash64c := make(chan []byte, 10)
Expand All @@ -467,6 +486,7 @@ func processStandardInput(output chan Result) {
sha3_256_c := make(chan []byte, 10)
sha3_384_c := make(chan []byte, 10)
sha3_512_c := make(chan []byte, 10)
ed2k_c := make(chan []byte, 10)

var wg sync.WaitGroup

Expand Down Expand Up @@ -606,6 +626,15 @@ func processStandardInput(output chan Result) {
wg.Done()
}()
}
if hasHash(HashNames.Ed2k) {
wg.Add(1)
go func() {
for b := range ed2k_c {
_, _ = ed2k_d.Write(b)
}
wg.Done()
}()
}

for {
n, err := r.Read(buf[:cap(buf)])
Expand Down Expand Up @@ -668,6 +697,9 @@ func processStandardInput(output chan Result) {
if hasHash(HashNames.Sha3512) {
sha3_512_c <- buf
}
if hasHash(HashNames.Ed2k) {
ed2k_c <- buf
}

if err != nil && err != io.EOF {
log.Fatal(err)
Expand All @@ -688,6 +720,7 @@ func processStandardInput(output chan Result) {
close(sha3_256_c)
close(sha3_384_c)
close(sha3_512_c)
close(ed2k_c)

wg.Wait()

Expand All @@ -708,6 +741,7 @@ func processStandardInput(output chan Result) {
Sha3256: encodeIfHashEnabled(sha3_256_d, HashNames.Sha3256),
Sha3384: encodeIfHashEnabled(sha3_384_d, HashNames.Sha3384),
Sha3512: encodeIfHashEnabled(sha3_512_d, HashNames.Sha3512),
Ed2k: encodeIfHashEnabled(ed2k_d, HashNames.Ed2k),
}

close(output)
Expand Down Expand Up @@ -918,6 +952,21 @@ func processReadFileParallel(filename string, content *[]byte) (Result, error) {
}()
}

if hasHash(HashNames.Ed2k) {
wg.Add(1)
go func() {
startTime = makeTimestampNano()
d := ed2k.New()
_, _ = d.Write(*content)
result.Ed2k = hex.EncodeToString(d.Sum(nil))

if Trace {
printTrace(fmt.Sprintf("nanoseconds processing ed2k: %s: %d", filename, makeTimestampNano()-startTime))
}
wg.Done()
}()
}

wg.Wait()
return result, nil
}
Expand Down Expand Up @@ -1080,6 +1129,17 @@ func processReadFile(filename string, content *[]byte) (Result, error) {
}
}

if hasHash(HashNames.Ed2k) {
startTime := makeTimestampNano()
d := ed2k.New()
_, _ = d.Write(*content)
result.Ed2k = hex.EncodeToString(d.Sum(nil))

if Trace {
printTrace(fmt.Sprintf("nanoseconds processing ed2k: %s: %d", filename, makeTimestampNano()-startTime))
}
}

return result, nil
}

Expand Down
9 changes: 9 additions & 0 deletions test-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ else
exit
fi

if echo "hello" | ./hashit --hash ed2k | grep -q -i '63481c78ae04c201fa01ea9d2b1db56d'; then
echo -e "${GREEN}PASSED stdin ed2k test"
else
echo -e "${RED}======================================================="
echo -e "FAILED Should be able to process ed2k stdin"
echo -e "======================================================="
exit
fi

a=$(./hashit --no-stream * | sort | md5sum)
b=$(./hashit * | sort | md5sum)
if [ "$a" == "$b" ]; then
Expand Down
Loading