-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathbyteencoder.go
47 lines (41 loc) · 1.31 KB
/
byteencoder.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
41
42
43
44
45
46
47
// Copyright 2020 Insolar Network Ltd.
// All rights reserved.
// This material is licensed under the Insolar License version 1.0,
// available at https://github.com/insolar/insolar/blob/master/LICENSE.md.
package reference
import (
"bytes"
"encoding/base64"
"io"
"strings"
base58 "github.com/jbenet/go-base58"
"github.com/pkg/errors"
)
type ByteEncodeFunc func(source io.ByteReader, builder *strings.Builder) error
func byteEncodeBase58(source io.ByteReader, builder *strings.Builder) error {
buff := bytes.Buffer{}
for b, err := source.ReadByte(); err == nil; b, err = source.ReadByte() {
err := buff.WriteByte(b)
if err != nil {
return errors.Wrap(err, "failed to write base58 encoded data to string builder")
}
}
_, err := builder.Write([]byte(base58.Encode(buff.Bytes())))
return err
}
func byteEncodeBase64(source io.ByteReader, builder *strings.Builder) error {
buff := bytes.Buffer{}
for b, err := source.ReadByte(); err == nil; b, err = source.ReadByte() {
buff.WriteByte(b)
}
encoder := base64.NewEncoder(base64.RawURLEncoding, builder)
_, err := encoder.Write(buff.Bytes())
if err != nil {
return errors.Wrap(err, "failed to write base64 encoded data to string builder")
}
err = encoder.Close()
if err != nil {
return errors.Wrap(err, "failed to close string builder")
}
return nil
}