v.0.0.4.3 Добавлено шифрование сообщения
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
434f97196f
commit
ca1771d761
@ -11,4 +11,8 @@ WHERE encrypted_key IS NULL OR nonce IS NULL;
|
||||
-- Делаем поля обязательными
|
||||
ALTER TABLE messages
|
||||
ALTER COLUMN encrypted_key SET NOT NULL,
|
||||
ALTER COLUMN nonce SET NOT NULL;
|
||||
ALTER COLUMN nonce SET NOT NULL;
|
||||
ALTER TABLE messages
|
||||
ALTER COLUMN content TYPE BYTEA USING content::bytea,
|
||||
ALTER COLUMN encrypted_key TYPE BYTEA USING encrypted_key::bytea,
|
||||
ALTER COLUMN nonce TYPE BYTEA USING nonce::bytea;
|
||||
@ -181,9 +181,9 @@ func (s *server) SendMessage(ctx context.Context, req *proto.SendMessageRequest)
|
||||
err = s.db.QueryRow(ctx, `
|
||||
INSERT INTO messages (chat_id, sender_id, receiver_id, content, encrypted_key, nonce)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id, chat_id, sender_id, receiver_id, content, status, created_at
|
||||
RETURNING id, chat_id, sender_id, receiver_id, status, created_at
|
||||
`, req.ChatId, req.SenderId, receiverId, encryptedContent, encryptedKey, nonce).Scan(
|
||||
&message.Id, &message.ChatId, &message.SenderId, &message.ReceiverId, &message.Content, &message.Status, &createdAt,
|
||||
&message.Id, &message.ChatId, &message.SenderId, &message.ReceiverId, &message.Status, &createdAt,
|
||||
)
|
||||
if err != nil {
|
||||
s.logger.Printf("Failed to insert message: %v", err)
|
||||
@ -347,8 +347,7 @@ func (s *server) GetChatMessages(ctx context.Context, req *proto.GetChatMessages
|
||||
for rows.Next() {
|
||||
var msg proto.Message
|
||||
var createdAt time.Time
|
||||
var encryptedKey, nonce []byte
|
||||
var encryptedContent string
|
||||
var encryptedKey, nonce, encryptedContent []byte
|
||||
|
||||
err := rows.Scan(
|
||||
&msg.Id, &msg.ChatId, &msg.SenderId, &msg.ReceiverId,
|
||||
@ -360,7 +359,7 @@ func (s *server) GetChatMessages(ctx context.Context, req *proto.GetChatMessages
|
||||
}
|
||||
|
||||
// Расшифровка сообщения
|
||||
decryptedContent, err := s.crypto.DecryptMessage([]byte(encryptedContent), encryptedKey, nonce)
|
||||
decryptedContent, err := s.crypto.DecryptMessage(encryptedContent, encryptedKey, nonce)
|
||||
if err != nil {
|
||||
s.logger.Printf("Failed to decrypt message %d: %v", msg.Id, err)
|
||||
msg.Content = "[encrypted - decryption failed]"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user