gmessages/libgm/crypto/encode.go
Tulir Asokan 1615e146b6 Update library
Co-authored-by: zero <108243503+0xzer@users.noreply.github.com>
2023-07-09 14:49:55 +03:00

78 lines
1.7 KiB
Go

package crypto
import (
"encoding/base64"
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/binary"
)
var SequenceOne = []int{1, 2, 840, 10045, 2, 1}
var SequenceTwo = []int{1, 2, 840, 10045, 3, 1, 7}
func EncodeBNA(a []byte) []byte {
b := 0
for b < len(a) && a[b] == 0 {
b++
}
c := 0
if b < len(a) && (a[b]&128) == 128 {
c = 1
}
d := make([]byte, len(a)-b+c)
copy(d[c:], a[b:])
return d
}
func EncodeValues(a *[]byte, b []int) {
*a = append(*a, 6)
idx := len(*a)
*a = append(*a, 0)
*a = append(*a, byte(40*b[0]+b[1]))
for i := 2; i < len(b); i++ {
d := b[i]
e := make([]byte, 0)
if d > 128 {
e = append(e, byte(d/128)+128)
}
e = append(e, byte(d%128))
*a = append(*a, e...)
}
(*a)[idx] = byte(len(*a) - idx - 1)
}
func AppendBytes(a []byte, b []byte) []byte {
newA := make([]byte, len(a))
copy(newA, a)
newA = HelperAppendBytes(newA, 48)
newA = HelperAppendBytes(newA, byte(len(b)))
for _, value := range b {
newA = HelperAppendBytes(newA, value)
}
return newA
}
func HelperAppendBytes(a []byte, b byte) []byte {
return append(a, b)
}
func AppendByteSequence(byteArr1 []byte, byteArr2 []byte, uncompressedPublicKey []byte) []byte {
copiedByteArray := AppendBytes(byteArr1, byteArr2)
copiedByteArray = HelperAppendBytes(copiedByteArray, 3)
copiedByteArray = HelperAppendBytes(copiedByteArray, uint8(len(uncompressedPublicKey)+1))
copiedByteArray = HelperAppendBytes(copiedByteArray, 0)
return copiedByteArray
}
func EncodeProtoB64(message proto.Message) (string, error) {
protoBytes, protoErr := binary.EncodeProtoMessage(message)
if protoErr != nil {
return "", protoErr
}
encodedStr := base64.StdEncoding.EncodeToString(protoBytes)
return encodedStr, nil
}