You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
775 B

  1. package crypto
  2. import "fmt"
  3. import "encoding/hex"
  4. const (
  5. ChecksumLength = 4 // for addresses
  6. HashLength = 32
  7. )
  8. type Hash [HashLength]byte
  9. type Checksum [ChecksumLength]byte
  10. func (h Hash) MarshalText() ([]byte, error) {
  11. return []byte(fmt.Sprintf("%x", h[:])), nil
  12. }
  13. // convert a hash of hex form to binary form, returns a zero hash if any error
  14. // TODO this should be in crypto
  15. func HashHexToHash(hash_hex string) (hash Hash) {
  16. hash_raw, err := hex.DecodeString(hash_hex)
  17. if err != nil {
  18. //panic(fmt.Sprintf("Cannot hex decode checkpint hash \"%s\"", hash_hex))
  19. return hash
  20. }
  21. if len(hash_raw) != 32 {
  22. //panic(fmt.Sprintf(" hash not 32 byte size Cannot hex decode checkpint hash \"%s\"", hash_hex))
  23. return hash
  24. }
  25. copy(hash[:], hash_raw)
  26. return
  27. }