{"id":15713698,"url":"https://github.com/watzon/nacl","last_synced_at":"2025-05-12T22:55:20.161Z","repository":{"id":91428228,"uuid":"192145862","full_name":"watzon/nacl","owner":"watzon","description":"Crystal bindings to libsodium (WIP)","archived":false,"fork":false,"pushed_at":"2019-06-17T01:01:09.000Z","size":83,"stargazers_count":12,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-12T22:55:14.953Z","etag":null,"topics":["blake2b","cryptography","crystal","crystal-lang","crystal-language","libsodium","xchacha20-poly1305"],"latest_commit_sha":null,"homepage":"https://watzon.github.io/nacl/","language":"Crystal","has_issues":true,"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/watzon.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-16T03:25:53.000Z","updated_at":"2022-10-12T23:28:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"c20bd26f-0e75-496b-add7-d2e9f741784f","html_url":"https://github.com/watzon/nacl","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watzon%2Fnacl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watzon%2Fnacl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watzon%2Fnacl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watzon%2Fnacl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/watzon","download_url":"https://codeload.github.com/watzon/nacl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253837400,"owners_count":21971982,"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":["blake2b","cryptography","crystal","crystal-lang","crystal-language","libsodium","xchacha20-poly1305"],"created_at":"2024-10-03T21:32:57.350Z","updated_at":"2025-05-12T22:55:20.135Z","avatar_url":"https://github.com/watzon.png","language":"Crystal","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NaCl\n\nFull Crystal binidings to [libsodium](https://libsodium.org). Very much a work in progress, but the lib bindings are there.\n\n## Installation\n\n1. Make sure you have `libsodium` installed on your system.\n\n```bash\n# Debian\nsudo apt install libsodium23\n\n# Arch\nsudo pacman -S libsodium\n\n# Fedora\nsudo yum install libsodium\n```\n\n2. Add the dependency to your `shard.yml`:\n\n   ```yaml\n   dependencies:\n     nacl:\n       github: watzon/nacl\n   ```\n\n2. Run `shards install`\n\n## Usage\n\n```crystal\nrequire \"nacl\"\n```\n\n### Secret Key Encryption\n\n#### XChaCha20Poly1305\n\n```crystal\n# Generate a random secret key\nkey = NaCl::AEAD::XChaCha20Poly1305.keygen\n\n# Initialize a XChaCha20Poly1305 cipher object\ncipher = NaCl::AEAD::XChaCha20Poly1305.new(key)\n\n# Generate a random nonce: a single-use value never repeated under the same key.\n# The nonce isn't secret, and can be sent with the ciphertext.\n# The cipher instance has a nonce_bytes method for determining how many bytes should be in a nonce.\nnonce = NaCl::Random.random_bytes(cipher.nonce_bytes)\n\n# Encrypt a message with XChaCha20Poly1305\nmessage = \"Crystal is amazing\" # Message to be encrypted\nad = \"\" # Additional data sent *in the clear* to be authenticated. This can be `nil`.\nciphertext = cipher.encrypt_string(nonce, message, ad)\n# =\u003e \"...\" string of random bytes, 16 bytes longer than the message.\n# The extra 16 bytes are the authenticator.\n\n# Decrypt a message, passing in the same additional data we used to encrypt.\ndecrypted_message = cipher.decrypt_string(nonce, ciphertext, ad)\n# =\u003e \"Crystal is amazing\"\n\n# But if the cipher has been tampered with:\ncipher.decrypt_string(nonce, corrupted_ciphertext, ad)\n# =\u003e NaCl::CryptoError\n\n# For encrypting bytes you can use:\nciphertext = cipher.encrypt(nonce, message.bytes, ad)\n\n# And to decrypt back to bytes\ndecrypted_bytes = cipher.decrypt(nonce, ciphertext, ad)\n# =\u003e Bytes[...]\n```\n\n### Digital Signatures\n\n#### Signer's Perspective\n\n```crystal\n# Generate a new random signing key\nsigning_key = NaCl::SigningKey.generate\n\n# Message to be signed\nmessage = \"Crystal is amazing\n\n# Sign a message with the signing key\nsignature = signing_key.sign(message)\n\n# Obtain the verify key for a given signing key\nverify_key = signing_key.verify_key\n\n# Convert the verify key to a string to send it to a third party\nverify_key.to_s\n```\n\n#### Verifier's Perspective\n\n```crystal\n# Create a VerifyKey object from a public key\nverify_key = NaCl::VerifyKey.new(verify_key.bytes)\n\n# Check the validity of a message's signature\n# Will raise NaCl::BadSignatureError if the signature check fails\nverify_key.verify(signature, message)\n```\n\n## Supported\n\n- [ ] SimpleBox (simplified cryptography)\n- [ ] [Secret-key Encryption](https://watzon.github.io/nacl/NaCl/AEAD.html)\n  - [ ] NaCl::SecretBox\n  - [x] [NaCl::AEAD::XChaCha20Poly1305](https://watzon.github.io/nacl/NaCl/AEAD/XChaCha20Poly1305.html)\n  - [ ] NaCl::AEAD::ChaCha20Poly1305IETF\n  - [ ] NaCl::AEAD::ChaCha20Poly1305Legacy\n- [ ] Public-key Encryption\n  - [ ] NaCl::Box\n  - [ ] NaCl::PrivateKey\n  - [ ] NaCl::PublicKey\n- [x] [Digital Signatures](https://watzon.github.io/nacl/NaCl/Signatures/Ed25519.html)\n  - [x] [NaCl::SigningKey](https://watzon.github.io/nacl/NaCl/Signatures/Ed25519/SigningKey.html)\n  - [x] [NaCl::VerifyKey](https://watzon.github.io/nacl/NaCl/Signatures/Ed25519/VerifyKey.html)\n- [ ] HMAC\n  - [ ] NaCl::HMAC::SHA256\n  - [ ] NaCl::HMAC::SHA512256\n- [ ] Hash Functions\n  - [x] [NaCl::Hash::Blake2b](https://watzon.github.io/nacl/NaCl/Hash/Blake2b.html)\n- [ ] Password Hashing\n  - [ ] NaCl::PasswordHash\n- [ ] Scalar Manipulation\n  - [ ] NaCl::GroupElement\n- [ ] One-time Authentication\n- [ ] Random Number Generation\n- [x] [Utilities](https://watzon.github.io/nacl/NaCl/Util.html)\n\n## Contributing\n\n1. Fork it (\u003chttps://github.com/watzon/nacl/fork\u003e)\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\n## Contributors\n\n- [Chris Watson](https://github.com/watzon) - creator and maintainer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwatzon%2Fnacl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwatzon%2Fnacl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwatzon%2Fnacl/lists"}