1
0

elgamal_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package elgamal
  5. import (
  6. "bytes"
  7. "crypto/rand"
  8. "math/big"
  9. "testing"
  10. )
  11. // This is the 1024-bit MODP group from RFC 5114, section 2.1:
  12. const primeHex = "B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C69A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C013ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD7098488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708DF1FB2BC2E4A4371"
  13. const generatorHex = "A4D1CBD5C3FD34126765A442EFB99905F8104DD258AC507FD6406CFF14266D31266FEA1E5C41564B777E690F5504F213160217B4B01B886A5E91547F9E2749F4D7FBD7D3B9A92EE1909D0D2263F80A76A6A24C087A091F531DBF0A0169B6A28AD662A4D18E73AFA32D779D5918D08BC8858F4DCEF97C2A24855E6EEB22B3B2E5"
  14. func fromHex(hex string) *big.Int {
  15. n, ok := new(big.Int).SetString(hex, 16)
  16. if !ok {
  17. panic("failed to parse hex number")
  18. }
  19. return n
  20. }
  21. func TestEncryptDecrypt(t *testing.T) {
  22. priv := &PrivateKey{
  23. PublicKey: PublicKey{
  24. G: fromHex(generatorHex),
  25. P: fromHex(primeHex),
  26. },
  27. X: fromHex("42"),
  28. }
  29. priv.Y = new(big.Int).Exp(priv.G, priv.X, priv.P)
  30. message := []byte("hello world")
  31. c1, c2, err := Encrypt(rand.Reader, &priv.PublicKey, message)
  32. if err != nil {
  33. t.Errorf("error encrypting: %s", err)
  34. }
  35. message2, err := Decrypt(priv, c1, c2)
  36. if err != nil {
  37. t.Errorf("error decrypting: %s", err)
  38. }
  39. if !bytes.Equal(message2, message) {
  40. t.Errorf("decryption failed, got: %x, want: %x", message2, message)
  41. }
  42. }