mirror of
https://github.com/h44z/wg-portal.git
synced 2025-09-15 07:11:15 +00:00
V2 alpha - initial version (#172)
Initial alpha codebase for version 2 of WireGuard Portal. This version is considered unstable and incomplete (for example, no public REST API)! Use with care! Fixes/Implements the following issues: - OAuth support #154, #1 - New Web UI with internationalisation support #98, #107, #89, #62 - Postgres Support #49 - Improved Email handling #47, #119 - DNS Search Domain support #46 - Bugfixes #94, #48 --------- Co-authored-by: Fabian Wechselberger <wechselbergerf@hotmail.com>
This commit is contained in:
67
internal/domain/crypto.go
Normal file
67
internal/domain/crypto.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
|
||||
type KeyPair struct {
|
||||
PrivateKey string
|
||||
PublicKey string
|
||||
}
|
||||
|
||||
func (p KeyPair) GetPrivateKeyBytes() []byte {
|
||||
data, _ := base64.StdEncoding.DecodeString(p.PrivateKey)
|
||||
return data
|
||||
}
|
||||
|
||||
func (p KeyPair) GetPublicKeyBytes() []byte {
|
||||
data, _ := base64.StdEncoding.DecodeString(p.PublicKey)
|
||||
return data
|
||||
}
|
||||
|
||||
func (p KeyPair) GetPrivateKey() wgtypes.Key {
|
||||
key, _ := wgtypes.ParseKey(p.PrivateKey)
|
||||
return key
|
||||
}
|
||||
|
||||
func (p KeyPair) GetPublicKey() wgtypes.Key {
|
||||
key, _ := wgtypes.ParseKey(p.PublicKey)
|
||||
return key
|
||||
}
|
||||
|
||||
type PreSharedKey string
|
||||
|
||||
func NewFreshKeypair() (KeyPair, error) {
|
||||
privateKey, err := wgtypes.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
return KeyPair{}, err
|
||||
}
|
||||
|
||||
return KeyPair{
|
||||
PrivateKey: privateKey.String(),
|
||||
PublicKey: privateKey.PublicKey().String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func NewPreSharedKey() (PreSharedKey, error) {
|
||||
preSharedKey, err := wgtypes.GenerateKey()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return PreSharedKey(preSharedKey.String()), nil
|
||||
}
|
||||
|
||||
func KeyBytesToString(key []byte) string {
|
||||
return base64.StdEncoding.EncodeToString(key)
|
||||
}
|
||||
|
||||
func PublicKeyFromPrivateKey(key string) string {
|
||||
privKey, err := wgtypes.ParseKey(key)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return privKey.PublicKey().String()
|
||||
}
|
Reference in New Issue
Block a user