diff options
Diffstat (limited to 'simulation/encoding.go')
-rw-r--r-- | simulation/encoding.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/simulation/encoding.go b/simulation/encoding.go new file mode 100644 index 0000000..98d1d4a --- /dev/null +++ b/simulation/encoding.go @@ -0,0 +1,63 @@ +package main + +import ( + "encoding/base32" + "errors" + "fmt" + "net/url" + "strings" +) + +// 32 characters for decoding, using RFC 3548. +const TALER_BASE32_CHARACTER_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" + +func talerBase32Encode(byts []byte) string { + return talerBase32Encoding().EncodeToString(byts) +} + +func talerBase32Decode(str string) ([]byte, error) { + + decoded, err := talerBase32Encoding().DecodeString(strings.ToUpper(str)) + if err != nil { + return nil, err + } + return decoded, nil +} + +func talerBase32Encoding() *base32.Encoding { + // 32 characters for decoding, using RFC 3548. + // character set copied from [TALER-EXCHANGE]/src/util/crypto_confirmation.c + return base32.NewEncoding(TALER_BASE32_CHARACTER_SET) +} + +func ParseWopid(wopid string) ([]byte, error) { + + unescaped, err := url.PathUnescape(wopid) + if err != nil { + fmt.Println("encoding", err) + return nil, errors.New("decoding failed") + } + + wopidBytes, err := talerBase32Decode(unescaped) + if err != nil { + return nil, err + } + + if len(wopidBytes) != 32 { + err = errors.New("invalid wopid") + fmt.Println("encoding", err) + return nil, err + } + + return wopidBytes, nil +} + +func FormatWopid(wopid []byte) string { + + return url.PathEscape(talerBase32Encode(wopid)) +} + +func ParseEddsaPubKey(key EddsaPublicKey) ([]byte, error) { + + return talerBase32Decode(string(key)) +} |