What I want is to send encypted data from my app to PostgreSQL database via formatted string (query) by using Npgsql. Problem is that after sha256.ComputeHash() I get byte array which I'm trying to send by inserting it into my string query. It works fine but then in my dbms column I see System.Byte[] instead of byte array. How can I fix that?
My string query is something like that:
private readonly SHA256 sha256 = SHA256.Create();
string sql = $"""
INSERT INT table(encrypted_column) VALUES (
'{sha256.ComputeHash(data)}');
""";
```[What in db after Query][1]
[1]: https://i.stack.imgur.com/9Ch7t.png
CodePudding user response:
SHA256.ComputeHash()
returns a byte array. It is converted to a string implicitly, using ToString()
. And it just returns the type string for byte array. You should use this query:
string sql = $"""INSERT INTO table (encrypted_column) VALUES ('{Encoding.UTF8.GetString(sha256.ComputeHash(data))}');""";
CodePudding user response:
So using a formatted string was a bad idea. Thanks to SNBS better way is to use parameters: https://www.npgsql.org/doc/basic-usage.html