mirror of
https://github.com/sosedoff/pgweb.git
synced 2024-12-14 19:21:46 +03:00
46 lines
793 B
Go
46 lines
793 B
Go
package client
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"fmt"
|
|
|
|
"github.com/mr-tron/base58"
|
|
)
|
|
|
|
const (
|
|
CodecNone = "none"
|
|
CodecHex = "hex"
|
|
CodecBase58 = "base58"
|
|
CodecBase64 = "base64"
|
|
)
|
|
|
|
var (
|
|
// BinaryCodec sets the serialization format of binary data
|
|
BinaryCodec = CodecBase64
|
|
)
|
|
|
|
func SetBinaryCodec(codec string) error {
|
|
switch codec {
|
|
case CodecNone, CodecHex, CodecBase58, CodecBase64:
|
|
BinaryCodec = codec
|
|
default:
|
|
return fmt.Errorf("invalid binary codec: %v", codec)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func encodeBinaryData(data []byte, codec string) string {
|
|
switch codec {
|
|
case CodecHex:
|
|
return hex.EncodeToString(data)
|
|
case CodecBase58:
|
|
return base58.Encode(data)
|
|
case CodecBase64:
|
|
return base64.StdEncoding.EncodeToString(data)
|
|
default:
|
|
return string(data)
|
|
}
|
|
}
|