dirhash 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 22b502bf40701ab13a91f36a748ebd7c4b78ad14f3e8a4d65c8b9e4fc1b54975
4
+ data.tar.gz: 359d3ddb5afcaf6b5c6cac6ab0e6f220a27c8dbd38fba592048fec39f55d016f
5
+ SHA512:
6
+ metadata.gz: d199de48ae3a80baa4e1a6b056c8cc2725affd1f442b0e5054fd6897d35d65a85ff7be85720928b91f13a16cbbb9081a962c3dbc2d81150859da3c873d5839b5
7
+ data.tar.gz: 65233d75725202d9c4c681857e7636e940f297fc2175bf63c256077ef9f71f5677b0efd89382b45e9d2dafb99ec08df6777d65dd9985ed314eea2d5f08b0c562
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-12-29
4
+
5
+ - Initial release
@@ -0,0 +1,10 @@
1
+ # Code of Conduct
2
+
3
+ "zipdigest" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
4
+
5
+ * Participants will be tolerant of opposing views.
6
+ * Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
7
+ * When interpreting the words and actions of others, participants should always assume good intentions.
8
+ * Behaviour which can be reasonably considered harassment will not be tolerated.
9
+
10
+ If you have any concerns about behaviour within this project, please contact us at ["andrewnez@gmail.com"](mailto:"andrewnez@gmail.com").
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Andrew Nesbitt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # Dirhash
2
+
3
+ Generate Go module zip digests compatible with sum.golang.org.
4
+
5
+ This gem computes hashes for Go module zip files using the same algorithm as Go's checksum database. You can verify module integrity or build tooling that works with Go's module ecosystem.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ gem install dirhash
11
+ ```
12
+
13
+ Or add to your Gemfile:
14
+
15
+ ```ruby
16
+ gem "dirhash"
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require "dirhash"
23
+
24
+ # Generate the h1: digest (compatible with go.sum)
25
+ digest = Dirhash.hash_zip("/path/to/module.zip")
26
+ # => "h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4="
27
+
28
+ # Generate the manifest (list of file hashes)
29
+ manifest = Dirhash.manifest("/path/to/module.zip")
30
+ ```
31
+
32
+ ## Hash Format
33
+
34
+ Go's sumdb uses a two-level hash scheme defined in [golang.org/x/mod/sumdb/dirhash](https://pkg.go.dev/golang.org/x/mod/sumdb/dirhash).
35
+
36
+ The manifest is built by:
37
+ 1. Listing all files in the zip (excluding directories)
38
+ 2. Sorting file names lexicographically
39
+ 3. For each file, computing `SHA256(content)` as lowercase hex
40
+ 4. Formatting each line as: `{hex_hash} {filename}\n` (two spaces between hash and name)
41
+
42
+ The final digest is:
43
+ 1. Concatenate all manifest lines
44
+ 2. Compute `SHA256(manifest)`
45
+ 3. Base64 encode the result
46
+ 4. Prefix with `h1:`
47
+
48
+ Example manifest:
49
+
50
+ ```
51
+ 2d7c3e5b... github.com/example/mod@v1.0.0/LICENSE
52
+ 8f4a2b1c... github.com/example/mod@v1.0.0/go.mod
53
+ a1b2c3d4... github.com/example/mod@v1.0.0/mod.go
54
+ ```
55
+
56
+ The `h1:` prefix indicates version 1 of the hash algorithm. Go reserves other prefixes for future algorithms.
57
+
58
+ ## Verifying Against sum.golang.org
59
+
60
+ You can verify a module by downloading it from proxy.golang.org and comparing:
61
+
62
+ ```ruby
63
+ require "dirhash"
64
+ require "net/http"
65
+ require "uri"
66
+
67
+ module_path = "github.com/pkg/errors"
68
+ version = "v0.9.1"
69
+
70
+ # Download the module zip
71
+ zip_url = "https://proxy.golang.org/#{module_path}/@v/#{version}.zip"
72
+ zip_data = Net::HTTP.get(URI(zip_url))
73
+ File.write("/tmp/module.zip", zip_data)
74
+
75
+ # Compute digest
76
+ digest = Dirhash.hash_zip("/tmp/module.zip")
77
+
78
+ # Fetch expected hash from sumdb
79
+ lookup_url = "https://sum.golang.org/lookup/#{module_path}@#{version}"
80
+ # Compare with the h1: hash in the response
81
+ ```
82
+
83
+ ## References
84
+
85
+ This implementation is based on:
86
+
87
+ - [foragepm/zipdigest](https://github.com/foragepm/zipdigest) - JavaScript implementation
88
+ - [golang.org/x/mod/sumdb/dirhash](https://pkg.go.dev/golang.org/x/mod/sumdb/dirhash) - Go's official implementation
89
+ - [Go Module Mirror and Checksum Database](https://sum.golang.org/) - The official sumdb service
90
+
91
+ ## Development
92
+
93
+ ```bash
94
+ bundle install
95
+ rake test
96
+ ```
97
+
98
+ ## License
99
+
100
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ task default: :test
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dirhash
4
+ VERSION = "0.1.0"
5
+ end
data/lib/dirhash.rb ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dirhash/version"
4
+ require "zip"
5
+ require "digest"
6
+ require "base64"
7
+
8
+ module Dirhash
9
+ class Error < StandardError; end
10
+
11
+ def self.hash_zip(zip_path)
12
+ manifest = manifest(zip_path)
13
+ hash = Digest::SHA256.digest(manifest)
14
+ "h1:" + Base64.strict_encode64(hash)
15
+ end
16
+
17
+ def self.manifest(zip_path)
18
+ entries = []
19
+
20
+ Zip::File.open(zip_path) do |zip_file|
21
+ zip_file.each do |entry|
22
+ next if entry.directory?
23
+ entries << entry.name
24
+ end
25
+ end
26
+
27
+ entries.sort!
28
+
29
+ lines = []
30
+ Zip::File.open(zip_path) do |zip_file|
31
+ entries.each do |name|
32
+ entry = zip_file.find_entry(name)
33
+ content = entry.get_input_stream.read
34
+ hash = Digest::SHA256.hexdigest(content)
35
+ lines << "#{hash} #{name}"
36
+ end
37
+ end
38
+
39
+ lines.join("\n") + "\n"
40
+ end
41
+ end
data/sig/dirhash.rbs ADDED
@@ -0,0 +1,9 @@
1
+ module Dirhash
2
+ VERSION: String
3
+
4
+ class Error < StandardError
5
+ end
6
+
7
+ def self.hash_zip: (String zip_path) -> String
8
+ def self.manifest: (String zip_path) -> String
9
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dirhash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Nesbitt
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rubyzip
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.3'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '2.3'
26
+ - !ruby/object:Gem::Dependency
27
+ name: base64
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ description: Generate digests and manifests of Go module zip contents using the same
41
+ algorithm as Go's sumdb dirhash
42
+ email:
43
+ - andrewnez@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CHANGELOG.md
49
+ - CODE_OF_CONDUCT.md
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - lib/dirhash.rb
54
+ - lib/dirhash/version.rb
55
+ - sig/dirhash.rbs
56
+ homepage: https://github.com/foragepm/dirhash-rb
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ homepage_uri: https://github.com/foragepm/dirhash-rb
61
+ source_code_uri: https://github.com/foragepm/dirhash-rb
62
+ changelog_uri: https://github.com/foragepm/dirhash-rb/blob/main/CHANGELOG.md
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 3.2.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 4.0.1
78
+ specification_version: 4
79
+ summary: Generate Go module zip digests compatible with sum.golang.org
80
+ test_files: []