encrypt.go 869 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package main
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "io"
  6. "log"
  7. "math/rand"
  8. "os"
  9. "github.com/certusone/wormhole/node/pkg/common"
  10. )
  11. func main() {
  12. in, err := io.ReadAll(os.Stdin)
  13. if err != nil {
  14. log.Fatalf("failed to read stdin: %v", err)
  15. }
  16. // Generate 128-bit key
  17. key := make([]byte, 16)
  18. if _, err := rand.Read(key); err != nil {
  19. log.Fatalf("failed to generate key: %v", err)
  20. }
  21. // Log key as base64 string
  22. log.Printf("key: %s", base64.StdEncoding.EncodeToString(key))
  23. // Encrypt
  24. ciphertext, err := common.EncryptAESGCM(in, key)
  25. if err != nil {
  26. log.Fatalf("failed to encrypt: %v", err)
  27. }
  28. // Convert ciphertext as base64 string.
  29. b64 := base64.StdEncoding.EncodeToString(ciphertext)
  30. // Hard-wrap to 80 characters per line.
  31. for i := 0; i < len(b64); i += 80 {
  32. j := i + 80
  33. if j > len(b64) {
  34. j = len(b64)
  35. }
  36. fmt.Println(b64[i:j])
  37. }
  38. }