diff --git a/assets/db/migrations.sql b/assets/db/migrations.sql index 813d070..d7216b9 100644 --- a/assets/db/migrations.sql +++ b/assets/db/migrations.sql @@ -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 ); diff --git a/assets/db/queries.sql b/assets/db/queries.sql index 6aaaf76..b635762 100644 --- a/assets/db/queries.sql +++ b/assets/db/queries.sql @@ -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 diff --git a/go.mod b/go.mod index bb31bb7..91bcc8f 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index c9440c7..0bb31d9 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/processor/database/db.go b/processor/database/db.go index dacb52e..85d4b8c 100644 --- a/processor/database/db.go +++ b/processor/database/db.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.27.0 +// sqlc v1.30.0 package database diff --git a/processor/database/models.go b/processor/database/models.go index 7a955ab..0433445 100644 --- a/processor/database/models.go +++ b/processor/database/models.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.27.0 +// sqlc v1.30.0 package database @@ -24,5 +24,6 @@ type FileHash struct { Sha3256 sql.NullString Sha3384 sql.NullString Sha3512 sql.NullString + Ed2k sql.NullString Size int64 } diff --git a/processor/database/queries.sql.go b/processor/database/queries.sql.go index 6565024..a01153f 100644 --- a/processor/database/queries.sql.go +++ b/processor/database/queries.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.27.0 +// sqlc v1.30.0 // source: queries.sql package database @@ -11,7 +11,7 @@ import ( ) const fileHashByFilePath = `-- name: FileHashByFilePath :one -select filepath, crc32, xxhash64, md4, md5, sha1, sha256, sha512, blake2b_256, blake2b_512, blake3, sha3_224, sha3_256, sha3_384, sha3_512, size from file_hashes where filepath = ? +select filepath, crc32, xxhash64, md4, md5, sha1, sha256, sha512, blake2b_256, blake2b_512, blake3, sha3_224, sha3_256, sha3_384, sha3_512, ed2k, size from file_hashes where filepath = ? ` func (q *Queries) FileHashByFilePath(ctx context.Context, filepath string) (FileHash, error) { @@ -33,6 +33,7 @@ func (q *Queries) FileHashByFilePath(ctx context.Context, filepath string) (File &i.Sha3256, &i.Sha3384, &i.Sha3512, + &i.Ed2k, &i.Size, ) return i, err @@ -42,10 +43,10 @@ const fileHashInsertReplace = `-- 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) -returning filepath, crc32, xxhash64, md4, md5, sha1, sha256, sha512, blake2b_256, blake2b_512, blake3, sha3_224, sha3_256, sha3_384, sha3_512, size +) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) +returning filepath, crc32, xxhash64, md4, md5, sha1, sha256, sha512, blake2b_256, blake2b_512, blake3, sha3_224, sha3_256, sha3_384, sha3_512, ed2k, size ` type FileHashInsertReplaceParams struct { @@ -64,6 +65,7 @@ type FileHashInsertReplaceParams struct { Sha3256 sql.NullString Sha3384 sql.NullString Sha3512 sql.NullString + Ed2k sql.NullString Size int64 } @@ -85,6 +87,7 @@ func (q *Queries) FileHashInsertReplace(ctx context.Context, arg FileHashInsertR arg.Sha3256, arg.Sha3384, arg.Sha3512, + arg.Ed2k, arg.Size, ) var i FileHash @@ -104,6 +107,7 @@ func (q *Queries) FileHashInsertReplace(ctx context.Context, arg FileHashInsertR &i.Sha3256, &i.Sha3384, &i.Sha3512, + &i.Ed2k, &i.Size, ) return i, err diff --git a/processor/formatters.go b/processor/formatters.go index 105fe1f..280e88b 100644 --- a/processor/formatters.go +++ b/processor/formatters.go @@ -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()) @@ -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()) @@ -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()) @@ -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 { @@ -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 { diff --git a/processor/processor.go b/processor/processor.go index c82169e..0eb2be3 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -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 @@ -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 diff --git a/processor/structs.go b/processor/structs.go index 1b274e5..e9d59f8 100644 --- a/processor/structs.go +++ b/processor/structs.go @@ -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"` } diff --git a/processor/workers.go b/processor/workers.go index 2ac30f8..ce77a27 100644 --- a/processor/workers.go +++ b/processor/workers.go @@ -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" ) @@ -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) @@ -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 @@ -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) @@ -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 @@ -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() @@ -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 } @@ -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) @@ -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 @@ -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)]) @@ -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) @@ -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() @@ -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) @@ -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 } @@ -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 } diff --git a/test-all.sh b/test-all.sh index 806fb24..68e70a9 100755 --- a/test-all.sh +++ b/test-all.sh @@ -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 diff --git a/vendor/go.felesatra.moe/hash/ed2k/LICENSE b/vendor/go.felesatra.moe/hash/ed2k/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/vendor/go.felesatra.moe/hash/ed2k/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/go.felesatra.moe/hash/ed2k/README.md b/vendor/go.felesatra.moe/hash/ed2k/README.md new file mode 100644 index 0000000..ff08326 --- /dev/null +++ b/vendor/go.felesatra.moe/hash/ed2k/README.md @@ -0,0 +1,3 @@ +# ed2k-go + +[![Go Reference](https://pkg.go.dev/badge/go.felesatra.moe/hash/ed2k.svg)](https://pkg.go.dev/go.felesatra.moe/hash/ed2k) diff --git a/vendor/go.felesatra.moe/hash/ed2k/ed2k.go b/vendor/go.felesatra.moe/hash/ed2k/ed2k.go new file mode 100644 index 0000000..df22fb7 --- /dev/null +++ b/vendor/go.felesatra.moe/hash/ed2k/ed2k.go @@ -0,0 +1,107 @@ +// Copyright (C) 2021 Allen Li +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package ed2k implements the eD2k hash. +// +// See https://en.wikipedia.org/wiki/Ed2k_URI_scheme. +package ed2k + +import ( + "hash" + + "golang.org/x/crypto/md4" +) + +// The blocksize of eD2k in bytes. +const BlockSize = md4.BlockSize + +// The size of an eD2k checksum in bytes. +const Size = md4.Size + +// The chunk size of eD2k in bytes. +const ChunkSize = 9728000 + +// A Hash is an implementation of hash.Hash for eD2k. +type Hash struct { + // Total bytes written so far. + written int64 + hashlist []byte + subhash hash.Hash +} + +// New returns a new Hash computing the eD2k checksum. +func New() *Hash { + return &Hash{ + subhash: md4.New(), + } +} + +// Write adds more data to the running hash. +// It never returns an error. +func (h *Hash) Write(p []byte) (int, error) { + total := len(p) + for len(p) > 0 { + p2 := h.limitNextChunk(p) + n, _ := h.subhash.Write(p2) + h.written += int64(n) + p = p[n:] + if h.written%ChunkSize == 0 { + h.hashlist = h.subhash.Sum(h.hashlist) + h.subhash.Reset() + } + } + return total, nil +} + +// limitNextChunk limits the input slice to at most the next chunk to write. +func (h *Hash) limitNextChunk(b []byte) []byte { + remainder := ChunkSize - int(h.written%ChunkSize) + if len(b) > remainder { + return b[:remainder] + } + return b +} + +// Sum appends the current hash to b and returns the resulting slice. +// It does not change the underlying hash state. +func (h *Hash) Sum(b []byte) []byte { + if h.written < ChunkSize { + return h.subhash.Sum(b) + } + if h.written == ChunkSize { + return append(b, h.hashlist...) + } + h2 := md4.New() + h2.Write(h.hashlist) + return h2.Sum(b) +} + +// Reset resets the Hash to its initial state. +func (h *Hash) Reset() { + h.written = 0 + h.hashlist = h.hashlist[:0] +} + +// Size returns the number of bytes Sum will return. +func (h *Hash) Size() int { + return Size +} + +// BlockSize returns the hash's underlying block size. +// The Write method must be able to accept any amount +// of data, but it may operate more efficiently if all writes +// are a multiple of the block size. +func (h *Hash) BlockSize() int { + return BlockSize +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 430fc99..b3de659 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -53,6 +53,9 @@ github.com/zeebo/blake3/internal/alg/hash/hash_avx2 github.com/zeebo/blake3/internal/alg/hash/hash_pure github.com/zeebo/blake3/internal/consts github.com/zeebo/blake3/internal/utils +# go.felesatra.moe/hash/ed2k v1.0.2 +## explicit; go 1.21.6 +go.felesatra.moe/hash/ed2k # golang.org/x/crypto v0.31.0 ## explicit; go 1.20 golang.org/x/crypto/md4