fix: use url-safe base64 encoding for noise

This commit is contained in:
Richard Ramos 2023-02-20 12:23:25 -04:00 committed by RichΛrd
parent 6e2a0ffeb4
commit 7bc000b61f
1 changed files with 23 additions and 11 deletions

View File

@ -2,7 +2,7 @@ package noise
import ( import (
"crypto/ed25519" "crypto/ed25519"
b64 "encoding/base64" "encoding/base64"
"errors" "errors"
"strings" "strings"
) )
@ -27,17 +27,29 @@ func NewQR(applicationName, applicationVersion, shardId string, ephemeralKey ed2
// Serializes input parameters to a base64 string for exposure through QR code (used by WakuPairing) // Serializes input parameters to a base64 string for exposure through QR code (used by WakuPairing)
func (qr QR) String() string { func (qr QR) String() string {
return b64.StdEncoding.EncodeToString([]byte(qr.applicationName)) + ":" + return base64.URLEncoding.EncodeToString([]byte(qr.applicationName)) + ":" +
b64.StdEncoding.EncodeToString([]byte(qr.applicationVersion)) + ":" + base64.URLEncoding.EncodeToString([]byte(qr.applicationVersion)) + ":" +
b64.StdEncoding.EncodeToString([]byte(qr.shardId)) + ":" + base64.URLEncoding.EncodeToString([]byte(qr.shardId)) + ":" +
b64.StdEncoding.EncodeToString(qr.ephemeralPublicKey) + ":" + base64.URLEncoding.EncodeToString(qr.ephemeralPublicKey) + ":" +
b64.StdEncoding.EncodeToString(qr.committedStaticKey[:]) base64.URLEncoding.EncodeToString(qr.committedStaticKey[:])
} }
func (qr QR) Bytes() []byte { func (qr QR) Bytes() []byte {
return []byte(qr.String()) return []byte(qr.String())
} }
func decodeBase64String(inputValue string) ([]byte, error) {
enc := base64.StdEncoding
if strings.ContainsAny(inputValue, "-_") {
enc = base64.URLEncoding
}
if len(inputValue)%4 != 0 {
enc = enc.WithPadding(base64.NoPadding)
}
return enc.DecodeString(inputValue)
}
// Deserializes input string in base64 to the corresponding (applicationName, applicationVersion, shardId, ephemeralKey, committedStaticKey) // Deserializes input string in base64 to the corresponding (applicationName, applicationVersion, shardId, ephemeralKey, committedStaticKey)
func StringToQR(qrString string) (QR, error) { func StringToQR(qrString string) (QR, error) {
values := strings.Split(qrString, ":") values := strings.Split(qrString, ":")
@ -45,27 +57,27 @@ func StringToQR(qrString string) (QR, error) {
return QR{}, errors.New("invalid qr string") return QR{}, errors.New("invalid qr string")
} }
applicationName, err := b64.StdEncoding.DecodeString(values[0]) applicationName, err := decodeBase64String(values[0])
if err != nil { if err != nil {
return QR{}, err return QR{}, err
} }
applicationVersion, err := b64.StdEncoding.DecodeString(values[1]) applicationVersion, err := decodeBase64String(values[1])
if err != nil { if err != nil {
return QR{}, err return QR{}, err
} }
shardId, err := b64.StdEncoding.DecodeString(values[2]) shardId, err := decodeBase64String(values[2])
if err != nil { if err != nil {
return QR{}, err return QR{}, err
} }
ephemeralKey, err := b64.StdEncoding.DecodeString(values[3]) ephemeralKey, err := decodeBase64String(values[3])
if err != nil { if err != nil {
return QR{}, err return QR{}, err
} }
committedStaticKey, err := b64.StdEncoding.DecodeString(values[4]) committedStaticKey, err := decodeBase64String(values[4])
if err != nil { if err != nil {
return QR{}, err return QR{}, err
} }