diff --git a/wgengine/wglog/wglog.go b/wgengine/wglog/wglog.go index 489d250db..f23b7973a 100644 --- a/wgengine/wglog/wglog.go +++ b/wgengine/wglog/wglog.go @@ -6,7 +6,6 @@ package wglog import ( - "encoding/base64" "fmt" "strings" "sync" @@ -121,17 +120,20 @@ func (x *Logger) SetPeers(peers []wgcfg.Peer) { // wireguardGoString prints p in the same format used by wireguard-go. func wireguardGoString(k wgkey.Key) string { - const prefix = "peer(" - b := make([]byte, len(prefix)+44) - copy(b, prefix) - r := b[len(prefix):] - base64.StdEncoding.Encode(r, k[:]) - r = r[4:] - copy(r, "…") - r = r[len("…"):] - copy(r, b[len(prefix)+39:len(prefix)+43]) - r = r[4:] - r[0] = ')' - r = r[1:] - return string(b[:len(b)-len(r)]) + src := k + b64 := func(input byte) byte { + return input + 'A' + byte(((25-int(input))>>8)&6) - byte(((51-int(input))>>8)&75) - byte(((61-int(input))>>8)&15) + byte(((62-int(input))>>8)&3) + } + b := []byte("peer(____…____)") + const first = len("peer(") + const second = len("peer(____…") + b[first+0] = b64((src[0] >> 2) & 63) + b[first+1] = b64(((src[0] << 4) | (src[1] >> 4)) & 63) + b[first+2] = b64(((src[1] << 2) | (src[2] >> 6)) & 63) + b[first+3] = b64(src[2] & 63) + b[second+0] = b64(src[29] & 63) + b[second+1] = b64((src[30] >> 2) & 63) + b[second+2] = b64(((src[30] << 4) | (src[31] >> 4)) & 63) + b[second+3] = b64((src[31] << 2) & 63) + return string(b) }