-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
33 lines (30 loc) · 868 Bytes
/
encode.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
package gosss
import (
"encoding/hex"
"math/big"
)
// shareToStr converts a big.Int to a string. It uses the bytes of the big.Int
func shareToStr(x, y *big.Int) (string, error) {
bx, by := x.Bytes(), y.Bytes()
lx, ly := len(bx), len(by)
if lx > 255 || ly > 255 {
return "", ErrShareTooLong
}
fullShare := append(bx, by...)
fullShare = append(fullShare, []byte{byte(lx), byte(ly)}...)
return hex.EncodeToString(fullShare), nil
}
// strToShare converts a string to a big.Int. It uses the bytes of the string
func strToShare(s string) (*big.Int, *big.Int, error) {
b, err := hex.DecodeString(s)
if err != nil {
return nil, nil, ErrInvalidShare
}
lb := len(b)
if len(b) < 2 {
return nil, nil, ErrInvalidShare
}
lx, ly := int(b[lb-2]), int(b[lb-1])
bx, by := b[:lx], b[lx:lx+ly]
return new(big.Int).SetBytes(bx), new(big.Int).SetBytes(by), nil
}