Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
2.6 KiB

  1. package Seeder
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/sha512"
  9. "encoding/pem"
  10. "hash"
  11. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  12. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
  13. "github.com/gofrs/uuid"
  14. )
  15. // EncryptWithPublicKey encrypts data with public key
  16. func encryptWithPublicKey(msg []byte, pub *rsa.PublicKey) []byte {
  17. var (
  18. hash hash.Hash
  19. )
  20. hash = sha512.New()
  21. ciphertext, err := rsa.EncryptOAEP(hash, rand.Reader, pub, msg, nil)
  22. if err != nil {
  23. panic(err)
  24. }
  25. return ciphertext
  26. }
  27. func PKCS5Padding(ciphertext []byte, blockSize int, after int) []byte {
  28. var (
  29. padding int
  30. padtext []byte
  31. )
  32. padding = (blockSize - len(ciphertext)%blockSize)
  33. padtext = bytes.Repeat([]byte{byte(padding)}, padding)
  34. return append(ciphertext, padtext...)
  35. }
  36. func seedMessage(primaryUser, secondaryUser Models.User, threadID uuid.UUID, i int) error {
  37. var (
  38. messageKey Models.Message
  39. messageData Models.MessageData
  40. block cipher.Block
  41. mode cipher.BlockMode
  42. pemBlock *pem.Block
  43. plaintext string
  44. ciphertext []byte
  45. bKey []byte
  46. bIV []byte
  47. bPlaintext []byte
  48. err error
  49. )
  50. plaintext = "Test Message"
  51. bKey = make([]byte, 32)
  52. _, err = rand.Read(bKey)
  53. if err != nil {
  54. panic(err)
  55. }
  56. bIV = make([]byte, 16)
  57. _, err = rand.Read(bIV)
  58. if err != nil {
  59. panic(err)
  60. }
  61. bPlaintext = PKCS5Padding([]byte(plaintext), aes.BlockSize, len(plaintext))
  62. pemBlock = &pem.Block{
  63. Type: "AES KEY",
  64. Bytes: bKey,
  65. }
  66. block, err = aes.NewCipher(bKey)
  67. if err != nil {
  68. panic(err)
  69. }
  70. ciphertext = make([]byte, len(bPlaintext))
  71. mode = cipher.NewCBCEncrypter(block, bIV)
  72. mode.CryptBlocks(ciphertext, bPlaintext)
  73. messageData = Models.MessageData{
  74. Data: ciphertext,
  75. }
  76. messageKey = Models.Message{
  77. UserID: primaryUser.ID,
  78. MessageData: messageData,
  79. MessageType: "sender",
  80. RelationalUserId: encryptWithPublicKey(secondaryUser.ID.Bytes(), decodedPublicKey),
  81. SymmetricKey: string(pem.EncodeToMemory(pemBlock)),
  82. }
  83. return Database.CreateMessage(&messageKey)
  84. }
  85. func SeedMessages() {
  86. var (
  87. primaryUser Models.User
  88. secondaryUser Models.User
  89. threadID uuid.UUID
  90. i int
  91. err error
  92. )
  93. primaryUser, err = Database.GetUserByUsername("testUser")
  94. if err != nil {
  95. panic(err)
  96. }
  97. secondaryUser, err = Database.GetUserByUsername("testUser2")
  98. if err != nil {
  99. panic(err)
  100. }
  101. threadID, err = uuid.NewV4()
  102. if err != nil {
  103. panic(err)
  104. }
  105. for i = 0; i <= 20; i++ {
  106. err = seedMessage(primaryUser, secondaryUser, threadID, i)
  107. if err != nil {
  108. panic(err)
  109. }
  110. }
  111. }