Kryptographie in Go (Golang): Verschlüsselung und Hashing

Online Python-Trainer für Anfänger

Lernen Sie Python einfach ohne theoretische Überlastung. Lösen Sie praktische Aufgaben mit automatischer Überprüfung und schreiben Sie Code direkt im Browser.

Kurs starten

Kryptographie in Go (Golang): vollständiger Leitfaden zur Verschlüsselung und zum Hashing



Go (Golang) bietet eine leistungsstarke Standardbibliothek crypto, die alle notwendigen Werkzeuge zur Implementierung kryptographischen Datenschutzes enthält. In diesem Artikel werden wir die grundlegenden Möglichkeiten der Arbeit mit Verschlüsselung und Hashing in Go im Detail besprechen, einschließlich symmetrischer (AES), asymmetrischer (RSA) Verschlüsselung, Hash-Funktionen (SHA-256) und Passwortsicherung mit bcrypt.



1. Daten-Hashing in Go



Hashing ist eine einseitige Umwandlung von Daten in eine feste Länge. In Go wird dafür das Paket crypto/sha256 (und andere SHA-Familien) verwendet.



Beispiel: SHA-256 Hash eines Strings



package main

import ( "crypto/sha256" "fmt")

func main() { data := "PythonLib.ru — Bildungswebsite" hash := sha256.Sum256([]byte(data)) fmt.Printf("SHA-256: %x", hash)}


Ausgabe: SHA-256: 3c4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4



Für Streaming-Hashing (große Dateien) verwenden Sie das hash.Hash Interface:



package main

import ( "crypto/sha256" "fmt" "io" "strings")

func main() { h := sha256.New() reader := strings.NewReader("Datenstrom zum Hashing") io.Copy(h, reader) fmt.Printf("Streaming SHA-256: %x", h.Sum(nil))}


2. Passwortsicherung mit bcrypt



Für die Speicherung von Passwörtern verwenden Sie niemals einfache Hashes (SHA-256). Verwenden Sie bcrypt – einen Algorithmus mit Salt und adaptiver Komplexität. In Go wird dies durch das Paket golang.org/x/crypto/bcrypt implementiert.



Installation:

go get golang.org/x/crypto/bcrypt


Beispiel für Hashing und Passwortüberprüfung:



package main

import ( "fmt" "golang.org/x/crypto/bcrypt")

func main() { password := "MySecurePass123!"

// Hashing (Standardkosten = 10) hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { panic(err) } fmt.Printf("bcrypt hash: %s", hash)

// Passwortüberprüfung err = bcrypt.CompareHashAndPassword(hash, []byte(password)) if err != nil { fmt.Println("Falsches Passwort") } else { fmt.Println("Passwort korrekt!") }}


3. Symmetrische Verschlüsselung AES-GCM



AES (Advanced Encryption Standard) ist ein symmetrischer Algorithmus. Der empfohlene Modus ist GCM (Galois/Counter Mode), der Authentifizierung (AEAD) bietet. Verwenden Sie die Pakete crypto/aes und crypto/cipher.



Beispiel für Verschlüsselung und Entschlüsselung AES-256-GCM:



package main

import ( "crypto/aes" "crypto/cipher" "crypto/rand" "encoding/hex" "fmt" "io")

func main() { key := []byte("32-Byte-Schlüssel für AES-256!!!") // 32 Bytes plaintext := []byte("Geheime Nachricht für PythonLib")

// Verschlüsselung ciphertext, nonce := encryptAESGCM(key, plaintext) fmt.Printf("Verschlüsselt: %s", hex.EncodeToString(ciphertext))

// Entschlüsselung decrypted, err := decryptAESGCM(key, ciphertext, nonce) if err != nil { panic(err) } fmt.Printf("Entschlüsselt: %s", decrypted)}

func encryptAESGCM(key, plaintext []byte) (ciphertext, nonce []byte) { block, _ := aes.NewCipher(key) gcm, _ := cipher.NewGCM(block) nonce = make([]byte, gcm.NonceSize()) io.ReadFull(rand.Reader, nonce) return gcm.Seal(nil, nonce, plaintext, nil), nonce}

func decryptAESGCM(key, ciphertext, nonce []byte) ([]byte, error) { block, _ := aes.NewCipher(key) gcm, _ := cipher.NewGCM(block) return gcm.Open(nil, nonce, ciphertext, nil)}


4. Asymmetrische Verschlüsselung RSA



RSA verwendet

Empfehlungen