-
Notifications
You must be signed in to change notification settings - Fork 0
/
coding.go
40 lines (36 loc) · 975 Bytes
/
coding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"encoding/base64"
"encoding/hex"
"fmt"
base58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
)
func Decode(encoding, digest string) (mh.Multihash, error) {
switch encoding {
case "raw":
return mh.Cast([]byte(digest))
case "hex":
return hex.DecodeString(digest)
case "base58":
return base58.Decode(digest), nil
case "base64":
return base64.StdEncoding.DecodeString(digest)
default:
return nil, fmt.Errorf("unknown encoding: %s", encoding)
}
}
func Encode(encoding string, hash mh.Multihash) (string, error) {
switch encoding {
case "raw":
return string(hash), nil
case "hex":
return hex.EncodeToString(hash), nil
case "base58":
return base58.Encode(hash), nil
case "base64":
return base64.StdEncoding.EncodeToString(hash), nil
default:
return "", fmt.Errorf("unknown encoding: %s", encoding)
}
}