Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to send encrypted byte array data in bytea column via C# Npgsql query

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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))}');""";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading