init
This commit is contained in:
89
crypto/crypto.go
Normal file
89
crypto/crypto.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/pem"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HashToken hashes a token using SHA-256.
|
||||||
|
func HashToken(token string) string {
|
||||||
|
hash := sha256.Sum256([]byte(token))
|
||||||
|
return hex.EncodeToString(hash[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// VerifyToken compares a plaintext token with a stored hash.
|
||||||
|
func VerifyToken(token, storedHash string) bool {
|
||||||
|
return HashToken(token) == storedHash
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() string {
|
||||||
|
// 1. Generiere einen privaten Schlüssel
|
||||||
|
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Fehler beim Generieren des privaten Schlüssels: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Extrahiere den öffentlichen Schlüssel
|
||||||
|
publicKey := &privateKey.PublicKey
|
||||||
|
|
||||||
|
// 3. Beispieltext zum Verschlüsseln
|
||||||
|
plainText := "Dies ist ein geheimer String!"
|
||||||
|
|
||||||
|
// 4. Verschlüssle den Text mit dem öffentlichen Schlüssel
|
||||||
|
encryptedBytes, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, []byte(plainText))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Fehler beim Verschlüsseln: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kodierung des verschlüsselten Textes in Base64 für Lesbarkeit
|
||||||
|
encryptedString := base64.StdEncoding.EncodeToString(encryptedBytes)
|
||||||
|
fmt.Println("Verschlüsselter Text (Base64):", encryptedString)
|
||||||
|
|
||||||
|
// 5. Entschlüssle den Text mit dem privaten Schlüssel
|
||||||
|
decryptedBytes, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, encryptedBytes)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Fehler beim Entschlüsseln: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. Gib den entschlüsselten Text aus
|
||||||
|
decryptedString := string(decryptedBytes)
|
||||||
|
fmt.Println("Entschlüsselter Text:", decryptedString)
|
||||||
|
|
||||||
|
// 7. (Optional) Exportiere und Importiere die Schlüssel
|
||||||
|
privateKeyPEM := exportPrivateKeyToPEM(privateKey)
|
||||||
|
fmt.Println("\nPrivate Key (PEM):\n", privateKeyPEM)
|
||||||
|
|
||||||
|
publicKeyPEM := exportPublicKeyToPEM(publicKey)
|
||||||
|
fmt.Println("\nPublic Key (PEM):\n", publicKeyPEM)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Funktion, um den privaten Schlüssel in PEM-Format zu exportieren
|
||||||
|
func exportPrivateKeyToPEM(privateKey *rsa.PrivateKey) string {
|
||||||
|
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
|
||||||
|
privateKeyPEM := pem.EncodeToMemory(&pem.Block{
|
||||||
|
Type: "RSA PRIVATE KEY",
|
||||||
|
Bytes: privateKeyBytes,
|
||||||
|
})
|
||||||
|
return string(privateKeyPEM)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Funktion, um den öffentlichen Schlüssel in PEM-Format zu exportieren
|
||||||
|
func exportPublicKeyToPEM(publicKey *rsa.PublicKey) string {
|
||||||
|
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Fehler beim Exportieren des öffentlichen Schlüssels: %v", err)
|
||||||
|
}
|
||||||
|
publicKeyPEM := pem.EncodeToMemory(&pem.Block{
|
||||||
|
Type: "PUBLIC KEY",
|
||||||
|
Bytes: publicKeyBytes,
|
||||||
|
})
|
||||||
|
return string(publicKeyPEM)
|
||||||
|
}
|
96
netproto/netproto.go
Normal file
96
netproto/netproto.go
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
package netproto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MainHeader struct {
|
||||||
|
Version uint8 `json:"version"`
|
||||||
|
Meta map[string]string `json:"meta"`
|
||||||
|
ExtensionHeaders []ExtensionHeader `json:"extension_headers"`
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
MainHeaderVersion1 = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ExtensionHeaderType_Source uint8 = 1 /* https://git.send.nrw/sendnrw/sendnrwlib/issues/1 */
|
||||||
|
ExtensionHeaderType_Destination uint8 = 2 /* https://git.send.nrw/sendnrw/sendnrwlib/issues/1 */
|
||||||
|
ExtensionHeaderType_PayloadDefinition uint8 = 3
|
||||||
|
ExtensionHeaderType_Protocol uint8 = 4
|
||||||
|
ExtensionHeaderType_Control uint8 = 5 /*Steuerbefehle wie Download, Upload, ...*/
|
||||||
|
ExtensionHeaderType_Crypto uint8 = 6 /*Hinweis auf eine Verschlüsselung*/
|
||||||
|
ExtensionHeaderType_Validation uint8 = 7 /*Hinweis auf eine Validierung oder Checksum*/
|
||||||
|
ExtensionHeaderType_Routing uint8 = 8 /*Hinweis auf eine Routing-Information*/
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateEHT1(IPv4, IPv6, Port string) ExtensionHeader {
|
||||||
|
return ExtensionHeader{
|
||||||
|
Type: ExtensionHeaderType_Source,
|
||||||
|
Meta: map[string]string{"version": "1.0"},
|
||||||
|
Data: map[string]string{"ipv4": IPv4, "ipv6": IPv6, "port": Port},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateEHT2(IPv4, IPv6, Port string) ExtensionHeader {
|
||||||
|
return ExtensionHeader{
|
||||||
|
Type: ExtensionHeaderType_Destination,
|
||||||
|
Meta: map[string]string{"version": "1.0"},
|
||||||
|
Data: map[string]string{"ipv4": IPv4, "ipv6": IPv6, "port": Port},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateEHT3(PayloadMime, PayloadLength, PayloadChecksum string) ExtensionHeader {
|
||||||
|
return ExtensionHeader{
|
||||||
|
Type: ExtensionHeaderType_PayloadDefinition,
|
||||||
|
Meta: map[string]string{"version": "1.0"},
|
||||||
|
Data: map[string]string{"payloadmime": PayloadMime, "payloadlength": PayloadLength, "payloadchecksum": PayloadChecksum},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateEHT4(ProtocolName, ProtocolVersion, ProtocolOID string) ExtensionHeader {
|
||||||
|
return ExtensionHeader{
|
||||||
|
Type: ExtensionHeaderType_Protocol,
|
||||||
|
Meta: map[string]string{"version": "1.0"},
|
||||||
|
Data: map[string]string{"protocolname": ProtocolName, "protocolversion": ProtocolVersion, "protocoloid": ProtocolOID},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateEHT7(Checksum string) ExtensionHeader {
|
||||||
|
return ExtensionHeader{
|
||||||
|
Type: ExtensionHeaderType_Validation,
|
||||||
|
Meta: map[string]string{"version": "1.0"},
|
||||||
|
Data: map[string]string{"checksum": Checksum},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateMH(Version uint8, Flag string) MainHeader {
|
||||||
|
return MainHeader{
|
||||||
|
Version: Version,
|
||||||
|
Meta: map[string]string{"version": "1.0", "flag": Flag},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func EncodeB64(input string) (string, error) {
|
||||||
|
return base64.StdEncoding.EncodeToString([]byte(input)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DecodeB64(input string) (string, error) {
|
||||||
|
d, err := base64.StdEncoding.DecodeString(input)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(d), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExtensionHeader struct {
|
||||||
|
Type uint8 `json:"type"`
|
||||||
|
Meta map[string]string `json:"meta"`
|
||||||
|
Data map[string]string `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type NetworkProtocol struct {
|
||||||
|
Header MainHeader `json:"header"`
|
||||||
|
Payload string `json:"payload"`
|
||||||
|
}
|
Reference in New Issue
Block a user