Last active
January 5, 2025 08:53
-
-
Save pjbgf/a90979582e339e262e298584295e0adf to your computer and use it in GitHub Desktop.
go-git API: sha256 Hash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hash | |
import ( | |
"encoding/hex" | |
"hash" | |
format "github.com/go-git/go-git/v5/plumbing/format/config" | |
) | |
// FromHex parses a hexadecimal string and returns an ImmutableHash | |
// and a boolean confirming whether the operation was successful. | |
// The hash (and object format) is inferred from the length of the | |
// input. | |
// | |
// If the operation was not successful, the resulting hash is nil | |
// instead of a zeroed hash. | |
func FromHex(in string) (StaticHash, bool) { | |
if len(in) < SHA1HexSize || | |
len(in) > SHA256HexSize { | |
return nil, false | |
} | |
b, err := hex.DecodeString(in) | |
if err != nil { | |
return nil, false | |
} | |
switch len(in) { | |
case SHA1HexSize: | |
h := SHA1Hash{} | |
copy(h.hash[:], b) | |
return h, true | |
case SHA256HexSize: | |
h := SHA256Hash{} | |
copy(h.hash[:], b) | |
return h, true | |
default: | |
return nil, false | |
} | |
} | |
// FromBytes creates an ImmutableHash object based on the value its | |
// Sum() should return. | |
// The hash type (and object format) is inferred from the length of | |
// the input. | |
// | |
// If the operation was not successful, the resulting hash is nil | |
// instead of a zeroed hash. | |
func FromBytes(in []byte) (StaticHash, bool) { | |
if len(in) < SHA1Size || | |
len(in) > SHA256Size { | |
return nil, false | |
} | |
switch len(in) { | |
case SHA1Size: | |
h := SHA1Hash{} | |
copy(h.hash[:], in) | |
return h, true | |
case SHA256Size: | |
h := SHA256Hash{} | |
copy(h.hash[:], in) | |
return h, true | |
default: | |
return nil, false | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hash | |
import ( | |
"bytes" | |
"crypto" | |
"encoding/hex" | |
) | |
const ( | |
// CryptoType defines what hash algorithm is being used. | |
CryptoType = crypto.SHA1 | |
// Size defines the amount of bytes the hash yields. | |
Size = SHA1Size | |
// HexSize defines the strings size of the hash when represented in hexadecimal. | |
HexSize = SHA1HexSize | |
) | |
type SHA1Hash struct { | |
hash [SHA1Size]byte | |
} | |
func (ih SHA1Hash) Size() int { | |
return len(ih.hash) | |
} | |
func (ih SHA1Hash) Empty() bool { | |
var empty SHA1Hash | |
return ih == empty | |
} | |
func (ih SHA1Hash) IsZero() bool { | |
return ih.Empty() | |
} | |
func (ih SHA1Hash) String() string { | |
return hex.EncodeToString(ih.hash[:]) | |
} | |
func (ih SHA1Hash) Sum() []byte { | |
return ih.hash[:] | |
} | |
func (ih SHA1Hash) Compare(in []byte) int { | |
return bytes.Compare(ih.hash[:], in) | |
} | |
func (ih SHA1Hash) HasPrefix(prefix []byte) bool { | |
return bytes.HasPrefix(ih.hash[:], prefix) | |
} | |
func (ih *SHA1Hash) Write(in []byte) (int, error) { | |
return copy(ih.hash[:], in), nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hash | |
import ( | |
"bytes" | |
"encoding/hex" | |
) | |
type SHA256Hash struct { | |
hash [SHA256Size]byte | |
} | |
func (ih SHA256Hash) Size() int { | |
return len(ih.hash) | |
} | |
func (ih SHA256Hash) Empty() bool { | |
var empty SHA256Hash | |
return ih == empty | |
} | |
func (ih SHA256Hash) IsZero() bool { | |
return ih.Empty() | |
} | |
func (ih SHA256Hash) String() string { | |
return hex.EncodeToString(ih.hash[:]) | |
} | |
func (ih SHA256Hash) Sum() []byte { | |
return ih.hash[:] | |
} | |
func (ih SHA256Hash) Compare(in []byte) int { | |
return bytes.Compare(ih.hash[:], in) | |
} | |
func (ih SHA256Hash) HasPrefix(prefix []byte) bool { | |
return bytes.HasPrefix(ih.hash[:], prefix) | |
} | |
func (ih *SHA256Hash) Write(in []byte) (int, error) { | |
return copy(ih.hash[:], in), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment