diff --git a/libgm/client.go b/libgm/client.go index b54aa06..ab04804 100644 --- a/libgm/client.go +++ b/libgm/client.go @@ -1,6 +1,7 @@ package textgapi import ( + "encoding/base64" "io" "log" "net/http" @@ -166,7 +167,7 @@ func (c *Client) decryptImages(messages *binary.FetchMessagesResponse) error { } func (c *Client) decryptImageData(imageId string, key []byte) ([]byte, error) { - decodedRpcKey, err := crypto.Base64DecodeStandard(c.rpcKey) + decodedRpcKey, err := base64.StdEncoding.DecodeString(c.rpcKey) if err != nil { return nil, err } @@ -192,7 +193,7 @@ func (c *Client) decryptImageData(imageId string, key []byte) ([]byte, error) { if err2 != nil { return nil, err2 } - download_metadata_b64 := crypto.EncodeBase64Standard(download_metadata_bytes) + download_metadata_b64 := base64.StdEncoding.EncodeToString(download_metadata_bytes) req, err := http.NewRequest("GET", util.UPLOAD_MEDIA, nil) if err != nil { return nil, err diff --git a/libgm/crypto/B64.go b/libgm/crypto/B64.go deleted file mode 100644 index f9f6627..0000000 --- a/libgm/crypto/B64.go +++ /dev/null @@ -1,41 +0,0 @@ -package crypto - -import ( - "encoding/base64" - "fmt" - "strings" -) - -func EncodeBase64Standard(data []byte) string { - return base64.StdEncoding.EncodeToString(data) -} - -func EncodeBase64(data []byte) string { - return base64.RawURLEncoding.EncodeToString(data) -} - -func Base64Decode(input string) ([]byte, error) { - padding := len(input) % 4 - if padding > 0 { - input += strings.Repeat("=", 4-padding) - } - - data, err := base64.URLEncoding.DecodeString(input) - if err != nil { - return nil, err - } - return data, nil -} - -func Base64DecodeStandard(input string) ([]byte, error) { - decoded, err := base64.StdEncoding.DecodeString(input) - if err != nil { - fmt.Println("decode error:", err) - return nil, err - } - return decoded, nil -} - -func Base64Encode(input []byte) string { - return base64.StdEncoding.EncodeToString(input) -} diff --git a/libgm/crypto/ECDSA.go b/libgm/crypto/ECDSA.go index 6daaf32..e7d4a3c 100644 --- a/libgm/crypto/ECDSA.go +++ b/libgm/crypto/ECDSA.go @@ -33,13 +33,13 @@ func (t *JWK) Marshal() ([]byte, error) { } func (t *JWK) PrivKeyB64Bytes() ([]byte, error) { - decodedPrivateKey, err2 := Base64Decode(t.D) + decodedPrivateKey, err2 := base64.RawURLEncoding.DecodeString(t.D) return decodedPrivateKey, err2 } func (t *JWK) ExtractPublicKeyDetails(pubKey []byte) *JWK { - x := EncodeBase64(pubKey[1:33]) - y := EncodeBase64(pubKey[33:]) + x := base64.RawURLEncoding.EncodeToString(pubKey[1:33]) + y := base64.RawURLEncoding.EncodeToString(pubKey[33:]) return &JWK{ Kty: "EC", Crv: "P-256", diff --git a/libgm/crypto/cryptor.go b/libgm/crypto/cryptor.go index 2a9dafe..ae917cd 100644 --- a/libgm/crypto/cryptor.go +++ b/libgm/crypto/cryptor.go @@ -6,6 +6,7 @@ import ( "crypto/hmac" "crypto/rand" "crypto/sha256" + "encoding/base64" "encoding/json" "errors" "io" @@ -35,7 +36,7 @@ func NewCryptor(aes_key []byte, sha_key []byte) *Cryptor { } func (c *Cryptor) SaveAsJson() { - AES_B64, SHA_B64 := EncodeBase64Standard(c.AES_CTR_KEY_256), EncodeBase64Standard(c.SHA_256_KEY) + AES_B64, SHA_B64 := base64.StdEncoding.EncodeToString(c.AES_CTR_KEY_256), base64.StdEncoding.EncodeToString(c.SHA_256_KEY) inter := struct { AES_CTR_KEY_256 string SHA_256_KEY string diff --git a/libgm/crypto/decode.go b/libgm/crypto/decode.go index ebbcb2d..0b9fffc 100644 --- a/libgm/crypto/decode.go +++ b/libgm/crypto/decode.go @@ -1,13 +1,15 @@ package crypto import ( + "encoding/base64" + "google.golang.org/protobuf/proto" "go.mau.fi/mautrix-gmessages/libgm/binary" ) func DecodeAndEncodeB64(data string, msg proto.Message) error { - decodedBytes, err := Base64DecodeStandard(data) + decodedBytes, err := base64.StdEncoding.DecodeString(data) if err != nil { return err } @@ -19,7 +21,7 @@ func DecodeAndEncodeB64(data string, msg proto.Message) error { } func DecodeEncodedResponse(data string) (*binary.EncodedResponse, error) { - decodedBytes, err := Base64DecodeStandard(data) + decodedBytes, err := base64.StdEncoding.DecodeString(data) if err != nil { return nil, err } diff --git a/libgm/crypto/encode.go b/libgm/crypto/encode.go index 36be9ee..2be921a 100644 --- a/libgm/crypto/encode.go +++ b/libgm/crypto/encode.go @@ -1,6 +1,8 @@ package crypto import ( + "encoding/base64" + "google.golang.org/protobuf/proto" "go.mau.fi/mautrix-gmessages/libgm/binary" @@ -55,6 +57,6 @@ func EncodeProtoB64(message proto.Message) (string, error) { if protoErr != nil { return "", protoErr } - encodedStr := EncodeBase64Standard(protoBytes) + encodedStr := base64.StdEncoding.EncodeToString(protoBytes) return encodedStr, nil } diff --git a/libgm/media_processor.go b/libgm/media_processor.go index 7e4eafc..7cc84b9 100644 --- a/libgm/media_processor.go +++ b/libgm/media_processor.go @@ -2,6 +2,7 @@ package textgapi import ( "bytes" + "encoding/base64" "errors" "io" "log" @@ -135,7 +136,7 @@ func (c *Client) StartUploadMedia(image *Image) (*StartGoogleUpload, error) { func (c *Client) buildStartUploadPayload() (string, error) { - decodedRpcKey, err := crypto.Base64DecodeStandard(c.rpcKey) + decodedRpcKey, err := base64.StdEncoding.DecodeString(c.rpcKey) if err != nil { return "", err } diff --git a/libgm/opcode_handler.go b/libgm/opcode_handler.go index 99e94d8..1bade3d 100644 --- a/libgm/opcode_handler.go +++ b/libgm/opcode_handler.go @@ -1,14 +1,14 @@ package textgapi import ( + "encoding/base64" "log" "go.mau.fi/mautrix-gmessages/libgm/binary" - "go.mau.fi/mautrix-gmessages/libgm/crypto" ) func (c *Client) handleSeperateOpCode(msgData *binary.MessageData) { - decodedBytes, err := crypto.Base64DecodeStandard(msgData.EncodedData) + decodedBytes, err := base64.StdEncoding.DecodeString(msgData.EncodedData) if err != nil { log.Fatal(err) } diff --git a/libgm/pair.go b/libgm/pair.go index 4a43828..d0fbb80 100644 --- a/libgm/pair.go +++ b/libgm/pair.go @@ -1,6 +1,7 @@ package textgapi import ( + "encoding/base64" "io" "log" "time" @@ -140,7 +141,7 @@ func (p *Pairer) GetWebEncryptionKey() { if err2 != nil { p.client.Logger.Err(err2).Msg("Parse webkeyresponse into proto struct error") } - key := crypto.EncodeBase64Standard(p.client.rpc.webAuthKey) + key := base64.StdEncoding.EncodeToString(p.client.rpc.webAuthKey) p.ticker.Stop() reconnectErr := p.client.Reconnect(key) if reconnectErr != nil { diff --git a/libgm/payload/refreshPhoneRelay.go b/libgm/payload/refreshPhoneRelay.go index 10e1af8..4b528bc 100644 --- a/libgm/payload/refreshPhoneRelay.go +++ b/libgm/payload/refreshPhoneRelay.go @@ -1,13 +1,14 @@ package payload import ( + "encoding/base64" + "go.mau.fi/mautrix-gmessages/libgm/binary" - "go.mau.fi/mautrix-gmessages/libgm/crypto" "go.mau.fi/mautrix-gmessages/libgm/util" ) func RefreshPhoneRelay(rpcKey string) ([]byte, *binary.Container, error) { - decodedRpcKey, err1 := crypto.Base64DecodeStandard(rpcKey) + decodedRpcKey, err1 := base64.StdEncoding.DecodeString(rpcKey) if err1 != nil { return nil, nil, err1 } diff --git a/libgm/qr.go b/libgm/qr.go index 828ffa7..00955f1 100644 --- a/libgm/qr.go +++ b/libgm/qr.go @@ -1,8 +1,9 @@ package textgapi import ( + "encoding/base64" + "go.mau.fi/mautrix-gmessages/libgm/binary" - "go.mau.fi/mautrix-gmessages/libgm/crypto" "go.mau.fi/mautrix-gmessages/libgm/util" ) @@ -16,6 +17,6 @@ func (p *Pairer) GenerateQRCodeData() (string, error) { if err != nil { return "", err } - cData := crypto.Base64Encode(encodedUrlData) + cData := base64.StdEncoding.EncodeToString(encodedUrlData) return util.QR_CODE_URL + cData, nil }