Simple tool to calc Golang module checksum of go.mod and module dir.
Simple tool to calc Golang module checksum of go.mod
and module directory
.
This README also describes how Golang go.sum calc hash from file conent.
Maybe this is the missing official doc for how Golang go.sum calc hash from file conent
git clone https://github.com/gin-gonic/gin.git
git checkout v1.4.0
go get -u github.com/vikyd/go-checksum
go.mod
go-checksum relOrAbsPathOfGinDir/go.mod
will print like:
file: /Users/viky/tmp/gin/go.mod
{
"Hash": "b1946b355a09fedc3865073bbeb5214de686542bbeaea9e1bf83cb3a08f66b99",
"HashBase64": "sZRrNVoJ/tw4ZQc7vrUhTeaGVCu+rqnhv4PLOgj2a5k=",
"HashSynthesized": "396d84667dc33bc2e7f6820a3af33ef8b04efb950f1c92431fbdbfabfdeb65d3",
"HashSynthesizedBase64": "OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=",
"GoCheckSum": "h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM="
}
The output hash "GoCheckSum": "h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM="
is the same as
the online checksum: github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
(from here)
go-checksum relOrAbsPathOfGinDir github.com/gin-gonic/[email protected]
will print like:
directory: /Users/viky/tmp/gin
{
"HashSynthesized": "ded3280827ccee9a6ab11d29b73ff08b58a6a4da53efff7042d319f25af59824",
"HashSynthesizedBase64": "3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ=",
"GoCheckSum": "h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ="
}
The output hash "GoCheckSum": "h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ="
is the same as
the online checksum: github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ=
(from here)
go.mod
checksum works?Steps:
go.mod
go.mod
, as variable content
content
, as variable hash
hash
, as variable mixedHash
hash go.mod\n
hash
is: CDa7N
mixedHash
is:CDa7N go.mod\n
mixedHash
, as variable hashSynthesized
hashSynthesized
, as variable hashSynthesizedBase64
go.sum
, the string is: h1:hashSynthesizedBase64
, as variable GoCheckSum
hashSynthesized
is: CCfM7
GoCheckSum
is: h1:CCfM7
h1
: The hash begins with an algorithm prefix of the form "h<N>:". The only defined algorithm prefix is "h1:", which uses SHA-256.
h
: hashSteps:
module's dir
and module's prefix
(also named import path)
module's prefix
is used to calc hash, as part of the content stringmodule's dir
module's dir
)module's prefix
, as variable fileImportPath
gin.go
/dir01/dir02/gin
/dir01/dir02/gin/gin.go
gin.go
fileImportPath
: github.com/gin-gonic/[email protected]/gin.go
fileImportPath
of all files, as variable files
.git
files
as string in increasion orderfiles
files
, as variable content
content
, as variable hash
hash
, as variable mixedHash
hash fileImportPath\n
hash
is: CDa7N
fileImportPath
is: github.com/gin-gonic/[email protected]/gin.go
mixedHash
is:CDa7N github.com/gin-gonic/[email protected]/gin.go\n
mixedHash
, as variable mixedHashAll
CDa7N github.com/gin-gonic/[email protected]/gin.go\nEFb8M github.com/gin-gonic/[email protected]/context.go\n ...
mixedHashAll
, as variable hashSynthesized
hashSynthesized
, as variable hashSynthesizedBase64
go.sum
, the string is: h1:hashSynthesizedBase64
, as variable GoCheckSum
hashSynthesized
is: CCfM7
GoCheckSum
is: h1:CCfM7
h1
: The hash begins with an algorithm prefix of the form "h<N>:". The only defined algorithm prefix is "h1:", which uses SHA-256.
h
: hashGolang source code about how to calc hash for modules: https://github.com/golang/mod/blob/release-branch.go1.15/sumdb/dirhash/hash.go