experimental-cli/project/decoders.go

44 lines
907 B
Go
Raw Permalink Normal View History

2020-09-10 12:33:41 +00:00
package main
import (
"strings"
2020-09-10 12:33:41 +00:00
)
type testCase struct {
decoded []byte
encoded string
}
// B32Decode takes a Crockford Base32 string and converts it
// to a byte array.
func B32Decode(input string) []byte {
output, error := encoder.DecodeString(input)
check(error, "Error decoding Base32 string %s", input)
2020-09-10 12:33:41 +00:00
return output
}
func decodeMhash(input string) []byte {
return []byte(B32Decode(input[5:]))
}
func isBlob(input string) bool {
if len(input) < 5 {
return false
}
2020-12-10 13:16:20 +00:00
return input[0:5] == BlobSigil
}
func validateMhash(input string) string {
2020-12-10 13:16:20 +00:00
array := strings.Split(input, ".")
if len(array) != 2 {
2020-09-24 23:43:11 +00:00
panicf("Expected '%s' to be an mHash", input)
}
2020-12-10 13:16:20 +00:00
switch array[0] + "." {
case BlobSigil, MessageSigil, PeerSigil:
return input
}
msg := "Expected left side of Mhash dot to be one of %s, %s, %s. Got: %s"
2020-12-10 13:16:20 +00:00
panicf(msg, BlobSigil, MessageSigil, PeerSigil, array[0])
return input
}