{"id":13661406,"url":"https://github.com/nxrighthere/Hydrogen-CSharp","last_synced_at":"2025-04-25T02:32:59.104Z","repository":{"id":55994104,"uuid":"159539918","full_name":"nxrighthere/Hydrogen-CSharp","owner":"nxrighthere","description":"Managed C# wrapper for Hydrogen cryptographic library by Frank Denis","archived":false,"fork":false,"pushed_at":"2022-03-13T12:04:14.000Z","size":42,"stargazers_count":32,"open_issues_count":0,"forks_count":6,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-08-02T05:13:09.941Z","etag":null,"topics":["cryptography","dotnet","encryption","hashing","interop","security"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nxrighthere.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-28T17:30:04.000Z","updated_at":"2024-03-19T21:03:23.000Z","dependencies_parsed_at":"2022-08-15T11:00:47.852Z","dependency_job_id":null,"html_url":"https://github.com/nxrighthere/Hydrogen-CSharp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nxrighthere%2FHydrogen-CSharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nxrighthere%2FHydrogen-CSharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nxrighthere%2FHydrogen-CSharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nxrighthere%2FHydrogen-CSharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nxrighthere","download_url":"https://codeload.github.com/nxrighthere/Hydrogen-CSharp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223979335,"owners_count":17235376,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cryptography","dotnet","encryption","hashing","interop","security"],"created_at":"2024-08-02T05:01:34.078Z","updated_at":"2024-11-10T16:30:32.664Z","avatar_url":"https://github.com/nxrighthere.png","language":"C#","funding_links":[],"categories":["C\\#","Game Development"],"sub_categories":["Unity Engine: Resources"],"readme":"\u003cp align=\"center\"\u003e \n  \u003cimg src=\"https://i.imgur.com/VsCHqUk.png\" alt=\"alt logo\"\u003e\n\u003c/p\u003e\n\nThis repository provides a managed C# wrapper for [Hydrogen](https://github.com/jedisct1/libhydrogen) cryptographic library which is created and maintained by [Frank Denis](https://github.com/jedisct1). You will need to [build](https://github.com/jedisct1/libhydrogen/wiki/Installation#downloading-the-source-code) the native library before you get started.\n\nBuilding\n--------\nA managed assembly can be built using any available compiling platform that supports C# 3.0 or higher.\n\nUsage\n--------\nBefore starting to work, the library should be initialized using `Hydrogen.Library.Initialize();` function.\n\n##### Generate random data\n```c#\n// Unbounded\nuint data = Hydrogen.Library.Random();\n\n// Bounded\nuint upperBound = 1000000;\nuint data = Hydrogen.Library.Random(upperBound);\n```\n\n##### Declare a new context\n```c#\n// Only the first 8 characters will be used\nstring context = \"hydrocontext\";\n```\n\n##### Generic hashing\n```c#\nstring message = \"Arbitrary data to hash\";\nbyte[] data = Encoding.ASCII.GetBytes(message);\nint hashLength = 16;\nbyte[] hash = new byte[hashLength]; // Storage for hash\n\n// Without a key\nif (Hydrogen.Library.Hash(hash, hashLength, data, data.Length, context))\n\tConsole.WriteLine(\"Hash successfully generated!\");\n  \n// With a key\nbyte[] hashKey = new byte[Hydrogen.Library.hashKeyBytes];\n\nHydrogen.Library.HashKeygen(hashKey);\n\nif (Hydrogen.Library.Hash(hash, hashLength, data, data.Length, context, hashKey))\n\tConsole.WriteLine(\"Hash successfully generated using key!\");\n```\n\n##### Password hashing\n```c#\nstring password = \"feelsgoodman\";\nbyte[] masterKey = new byte[Hydrogen.Library.masterKeyBytes];\nint keyLength = 32;\nbyte[] key = new byte[keyLength]; // Storage for high-entropy key\n\n// Generate master key\nHydrogen.Library.MasterKeygen(masterKey);\n\n// Generate high-entropy key from a password using master key\nif (Hydrogen.Library.DeterministicKey(key, keyLength, password, password.Length, context, masterKey, 1000, 1024, 1))\n\tConsole.WriteLine(\"High-entropy key successfully generated!\");\n\n// Generate authenticated representative of the password to store in the database\nbyte[] storedKey = new byte[Hydrogen.Library.storedBytes];\n\nif (Hydrogen.Library.StorageKey(storedKey, password, password.Length, masterKey, 1000, 1024, 1))\n\tConsole.WriteLine(\"Authenticated representative successfully generated!\");\n\n// Verify stored key\nif (Hydrogen.Library.VerifyKey(storedKey, password, password.Length, masterKey, 1000, 1024, 1))\n\tConsole.WriteLine(\"Stored key successfully verified!\");\n  \n// Reencrypt stored key\nbyte[] newMasterKey = new byte[Hydrogen.Library.masterKeyBytes];\n\nHydrogen.Library.MasterKeygen(newMasterKey);\n\nif (Hydrogen.Library.ReencryptKey(storedKey, masterKey, newMasterKey))\n\tConsole.WriteLine(\"Stored key successfully reencrypted!\");\n  \n// Upgrade stored key\nif (Hydrogen.Library.UpgradeKey(storedKey, newMasterKey, 2000, 1024, 2))\n\tConsole.WriteLine(\"Stored key successfully upgraded!\");\n```\n\n##### Secret key encryption\n```c#\nbyte[] key = new byte[Hydrogen.Library.secretKeyBytes]; // Storage for secret key\n\n// Generate secret key\nHydrogen.Library.SecretKeygen(key);\n\n// Secret data\nstring message = \"Secret message for future generations\";\nbyte[] data = Encoding.ASCII.GetBytes(message);\n\n// Encrypt data\nbyte[] cipher = new byte[data.Length + Hydrogen.Library.headerBytes];\n\nif (Hydrogen.Library.Encrypt(cipher, data, data.Length, context, key))\n\tConsole.WriteLine(\"Data successfully encrypted!\");\n\n// Create probe for cipher\nbyte[] probe = new byte[Hydrogen.Library.probeBytes];\n\nif (Hydrogen.Library.CreateProbe(probe, cipher, cipher.Length, context, key))\n\tConsole.WriteLine(\"Probe successfully created!\");\n\t\n// Verify probe\nif (Hydrogen.Library.VerifyProbe(probe, cipher, cipher.Length, context, key))\n\tConsole.WriteLine(\"Probe successfully verified!\");\n\n// Decrypt data\nbyte[] data = new byte[cipher.Length - Hydrogen.Library.headerBytes];\n\nif (Hydrogen.Library.Decrypt(data, cipher, cipher.Length, context, key))\n\tConsole.WriteLine(\"Data successfully decrypted!\");\n```\n\n##### Secure network communication based on the Noise protocol (N variant)\n```c#\n// Server\nKeyPair serverKeyPair = default(KeyPair);\n\n// Generate long-term key pair\nHydrogen.Library.ExchangeKeygen(out serverKeyPair);\n\n/* Send `serverKeyPair.publicKey` to the client */\n\n// Client\nSessionKeyPair clientSessionKeyPair = default(SessionKeyPair);\nbyte[] packet = new byte[Hydrogen.Library.packetBytes];\n\n// Generate session keys and a packet with an ephemeral public key\nif (Hydrogen.Library.N1(out clientSessionKeyPair, packet, serverKeyPair.publicKey))\n\tConsole.WriteLine(\"Session key pair successfully generated!\");\n\n/* Send `packet` to the server */\n\n// Server\nSessionKeyPair serverSessionKeyPair = default(SessionKeyPair);\n\n// Process the initial request from the client and generate session keys\nif (Hydrogen.Library.N2(out serverSessionKeyPair, packet, ref serverKeyPair))\n\tConsole.WriteLine(\"Session key pair successfully generated!\");\n\n/* Send a signal to the client that secure communication is established */\n\n// Client\nstring message = \"Do you want to take a look at my high-poly things tonight?\";\nbyte[] data = Encoding.ASCII.GetBytes(message);\nbyte[] packet = new byte[data.Length + Hydrogen.Library.headerBytes];\n\n// Encrypt data\nif (Hydrogen.Library.Encrypt(packet, data, data.Length, context, clientSessionKeyPair.sendKey))\n\tConsole.WriteLine(\"Data successfully encrypted!\");\n\n/* Send `packet` to the server */\n\n// Server\nbyte[] data = new byte[packet.Length - Hydrogen.Library.headerBytes];\n\n// Decrypt data\nif (Hydrogen.Library.Decrypt(data, packet, packet.Length, context, serverSessionKeyPair.receiveKey))\n\tConsole.WriteLine(\"Data successfully decrypted!\");\n\nConsole.WriteLine(\"Received message: \" + Encoding.ASCII.GetString(data));\n```\n\n##### Secure network communication based on the Noise protocol (KK variant)\n```c#\n// Client\nKeyPair clientKeyPair = default(KeyPair);\n\n// Generate long-term key pair\nHydrogen.Library.ExchangeKeygen(out clientKeyPair);\n\n// Server\nKeyPair serverKeyPair = default(KeyPair);\n\n// Generate long-term key pair\nHydrogen.Library.ExchangeKeygen(out serverKeyPair);\n\n/* Send `serverKeyPair.publicKey` to the client */\n\n// Client\nKeyState clientState = default(KeyState);\nbyte[] initialPacket = new byte[Hydrogen.Library.packetBytes];\n\n// Initiate a key exchange\nif (Hydrogen.Library.KK1(out clientState, initialPacket, serverKeyPair.publicKey, ref clientKeyPair))\n\tConsole.WriteLine(\"Initial packet successfully generated!\");\n\n/* Send `initialPacket` to the server */\n\n// Server\nSessionKeyPair serverSessionKeyPair = default(SessionKeyPair);\nbyte[] packet = new byte[Hydrogen.Library.packetBytes];\n\n// Process the initial request from the client, and generate session keys\nif (Hydrogen.Library.KK2(out serverSessionKeyPair, packet, initialPacket, clientKeyPair.publicKey, ref serverKeyPair))\n\tConsole.WriteLine(\"Session key pair successfully generated!\");\n\n/* Send `packet` to the client */\n\n// Client\nSessionKeyPair clientSessionKeyPair = default(SessionKeyPair);\n\n// Process the server packet and generate session keys\nif (Hydrogen.Library.KK3(ref clientState, out clientSessionKeyPair, packet, ref clientKeyPair))\n\tConsole.WriteLine(\"Session key pair successfully generated!\");\n\n/* Send a signal to the server that secure communication is established */\n\n// Client\nstring message = \"Hold my beer\";\nbyte[] data = Encoding.ASCII.GetBytes(message);\nbyte[] packet = new byte[data.Length + Hydrogen.Library.headerBytes];\n\n// Encrypt data\nif (Hydrogen.Library.Encrypt(packet, data, data.Length, context, clientSessionKeyPair.sendKey))\n\tConsole.WriteLine(\"Data successfully encrypted!\");\n\n/* Send `packet` to the server */\n\n// Server\nbyte[] data = new byte[packet.Length - Hydrogen.Library.headerBytes];\n\n// Decrypt data\nif (Hydrogen.Library.Decrypt(data, packet, packet.Length, context, serverSessionKeyPair.receiveKey))\n\tConsole.WriteLine(\"Data successfully decrypted!\");\n\nConsole.WriteLine(\"Received message: \" + Encoding.ASCII.GetString(data));\n```\n\n##### Public/Private key signatures\n```c#\nSignKeyPair keyPair = default(SignKeyPair);\n\n// Generate key pair\nHydrogen.Library.SignKeygen(out keyPair);\n\nbyte[] signature = new byte[signBytes];\n\nstring message = \"'You can't give her that!' she screamed. 'It's not safe!' IT'S A SWORD, said the Hogfather. THEY'RE NOT MEANT TO BE SAFE.\";\nbyte[] data = Encoding.ASCII.GetBytes(message);\n\n// Sign the message\nif(Hydrogen.Library.SignCreate(signature, data, data.Length, context, keyPair.secretKey))\n\tConsole.WriteLine(\"Message successfully signed!\");\n\n// Verify the signature\nif(Hydrogen.Library.SignVerify(signature, data, data.Length, context, keyPair.publicKey))\n\tConsole.WriteLine(\"Message signature successfully verified!\");\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnxrighthere%2FHydrogen-CSharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnxrighthere%2FHydrogen-CSharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnxrighthere%2FHydrogen-CSharp/lists"}