go.sum 中的校验和是如何计算的?

问题描述 投票:0回答:2

我查看了https://go.dev/doc/modules/gomod-refhttps://go.dev/ref/mod#go-mod-tidy,在这两个页面上我都找不到任何解释如何计算

go.sum
中的校验和的文档。

go.sum
中的校验和是如何计算的?

go checksum go-modules
2个回答
2
投票

校验和是依赖项的哈希值。您要查找的文档是 https://go.dev/ref/mod#go-sum-files

go.sum 中的每一行都有三个用空格分隔的字段:模块路径、版本(可能以 /go.mod 结尾)和哈希值。

  • 模块路径是哈希所属模块的名称。
  • 版本是哈希所属模块的版本。如果版本以 /go.mod 结尾,则哈希值仅适用于模块的 go.mod 文件;否则,哈希值适用于模块的 .zip 文件中的文件。
  • 哈希列由算法名称(如 h1)和 Base64 编码的加密哈希组成,以冒号 (:) 分隔。目前,SHA-256 (h1) 是唯一支持的哈希算法。如果将来发现 SHA-256 中的漏洞,将添加对另一种算法(名为 h2 等)的支持。

带有

module version hash
的 go.sum 行示例就像

github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs=
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg=

0
投票

如果您有兴趣手动计算/验证

checksums
,您可以参考我的答案这里

通过传递下载模块的

bash
cache
来使用
prefix
::

 harsha $ sha256sum $(find /Users/hmusanalli/go/pkg/mod/golang.org/x/[email protected] -type f | sort ) | sed 's#/Users/hmusanalli/go/pkg/mod/##' | sha256sum | xxd -r -p | base64       
 CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=

go
使用定义的内置
HashDir
函数here::

func TestHashDir2(t *testing.T) {
  out, err := HashDir("/Users/hmusanalli/go/pkg/mod/golang.org/x/[email protected]", "golang.org/x/[email protected]", Hash1)
  if err != nil {
    t.Fatalf("HashDir: %v", err)
  }

  want := "h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o="
  if out != want {
    t.Errorf("HashDir(...) = %s, want %s", out, want)
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.