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)
|
||||
}
|
Reference in New Issue
Block a user