{"id":16965038,"url":"https://github.com/propensive/gastronomy","last_synced_at":"2025-03-17T08:37:46.511Z","repository":{"id":37234579,"uuid":"157044593","full_name":"propensive/gastronomy","owner":"propensive","description":"Simple generically-derived cryptographic functions for Scala","archived":false,"fork":false,"pushed_at":"2025-01-26T12:14:10.000Z","size":10062,"stargazers_count":20,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-10T19:53:47.386Z","etag":null,"topics":["cryptographic-digests","cryptography","digest","encryption","hash","md5","scala","sha2","sha256"],"latest_commit_sha":null,"homepage":"https://propensive.com/gastronomy/","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/propensive.png","metadata":{"files":{"readme":".github/readme.md","changelog":null,"contributing":".github/contributing.md","funding":null,"license":null,"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":"2018-11-11T03:41:12.000Z","updated_at":"2025-01-26T12:14:14.000Z","dependencies_parsed_at":"2023-11-16T09:25:45.557Z","dependency_job_id":"df969ba7-1f13-4951-b48e-5c8529b2347b","html_url":"https://github.com/propensive/gastronomy","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fgastronomy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fgastronomy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fgastronomy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fgastronomy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/propensive","download_url":"https://codeload.github.com/propensive/gastronomy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243852499,"owners_count":20358271,"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":["cryptographic-digests","cryptography","digest","encryption","hash","md5","scala","sha2","sha256"],"created_at":"2024-10-13T23:44:52.918Z","updated_at":"2025-03-17T08:37:46.474Z","avatar_url":"https://github.com/propensive.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"[\u003cimg alt=\"GitHub Workflow\" src=\"https://img.shields.io/github/actions/workflow/status/propensive/gastronomy/main.yml?style=for-the-badge\" height=\"24\"\u003e](https://github.com/propensive/gastronomy/actions)\n[\u003cimg src=\"https://img.shields.io/discord/633198088311537684?color=8899f7\u0026label=DISCORD\u0026style=for-the-badge\" height=\"24\"\u003e](https://discord.com/invite/MBUrkTgMnA)\n\u003cimg src=\"/doc/images/github.png\" valign=\"middle\"\u003e\n\n# Gastronomy\n\n__Simple generically-derived cryptographic digestion__\n\nGastronomy provides a range of common cryptographic operations through a simple, typesafe and\nimmutable API.\n\n## Features\n\n- hashing of simple and primitive Scala types\n- generically-derived digests for all product and coproduct types\n- supports SHA-256, SHA-1 and MD5 hash algorithms\n- symmetric encryption with AES\n- asymmetric encryption/decryption using RSA\n- signing with DSA\n- AES, RSA and DSA key generation\n- calculation of HMACs for SHA-256, SHA-1 and MD5\n- encoding into Hex, BASE-64, and URL-safe BASE-64\n- serializers and parsers for PEM-encoded data\n\n## Availability\n\n\n\n\n\n\n\n## Getting Started\n\nAll Gastronomy terms and types are defined in the `gastronomy` package:\n```scala\nimport gastronomy.*\n```\nand exported to the `soundness` package:\n```scala\nimport soundness.*\n```\n\n__Gastronomy__ provides representations of public, private and symmetric keys which offer a number\nof cryptographic methods:\n\n- `PublicKey` provides:\n  - `verify` for verifying signatures\n  - `encrypt` for encrypting data\n  - `pem` to provide the public key as a PEM-encoded string\n- `PrivateKey` provides:\n  - `sign` for signing data\n  - `decrypt` for decrypting encrypted data\n  - `public` to derive a `PublicKey` from the `PrivateKey`\n  - `pem` to provide the private key as a PEM-encoded string\n- `SymmetricKey` provides `verify`, `encrypt`, `pem`, `sign` and `decrypt` in a single key.\n\nAdditionally, the extension methods, `digest` and `hmac` are provided for any value which can be\nserialized to bytes.\n\nThe objects `PrivateKey` and `SymmetricKey` both have `generate` methods which will generate new\nrandom keys.\n\n### Signing\n\nGiven, for example, a `PrivateKey[Dsa[1024]]` instance, `key`, data may be signed with, for example,\n```scala\nval key: PrivateKey[Dsa[1024]] = ???\nval signature: Signature[Dsa[1024]] = key.sign(data)\n```\nThis works for any value, `data`, that has an appropriate `ByteCodec` instance. The type parameter\nof the signature will depend on the type parameter of the private key.\n\n### Verifying a signature\n\nA public key, `pubKey`, which could, for example, be derived from the private key in the previous\nexample,\n```scala\nval pubKey = key.public\n```\nmay be used to verify a signature of type `Signature[Dsa[1024]]` with:\n```scala\nval valid: Boolean = pubKey.verify(data, signature)\n```\n\nHere, `data` must be the same object that was used (with the private key) to produce the signature,\nand may be any type that has a contextual `ByteCodec` instance.\n\n### Encryption\n\nA public key instance, for example, `pubKey` of type `PublicKey[Rsa[2048]]`, can encrypt some data\nby calling,\n```scala\nval encrypted: Message[Rsa[2048]] = pubKey.encrypt(data)\n```\n\n### Decryption\n\nAn encrypted message may conversely be decrypted using the corresponding `PrivateKey[Rsa[2048]]`\ninstance, `key`:\n```scala\nval data: String = key.decrypt[String](encrypted)\n```\n\nThe return type (`String` in the above example) must be specified as a parameter to the `decrypt`\nmethod, and may be any type for which a corresponding `ByteCodec` exists in context. However, the\ntype should be the same as the type of the object that was originally encrypted, otherwise it may\nfail to decode.\n\n### Digests\n\nA cryptographic digest (or hash) of any value may be calculated by calling `digest[A]` on that\nvalue, for an appropriate choice of `A`, provided a `Hashable` instance is in context for that type\nof object. `Hashable` instances exist for `String`s, primitive types, sequences of these types, and\nproduct and coproduct types consisting of just other hashable types.\n\nCryptographic digests have the type `Digest[A]` where `A` is the algorithm type.\n\nFor example,\n```scala\nval digest: Digest[Sha2[384]] = (10, \"alpha\", 'z').digest[Sha2[384]]\n```\n\n### HMACs\n\nAny value whose type has a corresponding `ByteCodec` instance in context may have an HMAC value\ncalculated, of type `Hmac[A]` (where `A` is the cryptographic algorithm). As a parameter, this\nneeds an `IArray[Byte]` representing (in some form) the key to be used.\n\nHere is an example using SHA-512:\n```scala\nval hmac: Hmac[Sha2[512]] = \"Hello world\".hmac(\"secret\".bytes)\n```\n\n### Type inference\n\nWhenever an expression is used in a position with an expected type, the type parameters of the\nmethods `decrypt`, `digest` and `hmac` may be omitted, for example given the case class,\n```scala\ncase class Block(digest: Digest[Sha2[256]], json: Json, hmac: Hmac[Sha2[512]])\n```\nwe can instantiate it with just,\n```scala\nval block = Block(data.digest, data.decrypt, value.hmac)\n```\n\nAlternatively, a particular given may be imported directly into the current scope to prioritize it,\nsuch that it may be used in preference to the alternatives.\n\n### Byte data\n\nRepresentations of binary data are common with low-level cryptographic operations. All operations in\nGastronomy use the immutable `IArray[Byte]` type as the underlying representation of binary data,\nbut typically wrap the data in a type which more precisely indicates the content of that data.\n\nThese types include the key types, `PublicKey`, `PrivateKey` and `SymmetricKey`, and result types,\n`Signature`, `Hmac`, `Digest` and `Message`.\n\nThese types are all further refined with a type parameter representing the cryptographic algorithm\nassociated with that data. For example, an MD5 digest is typed as, `Digest[Md5]` and a 384-bit SHA-2\nHMAC has the type, `Hmac[Sha2[384]]`.\n\nIn order to make it easier to share these values, they can be encoded to and from `String`s using\na number of different encodings:\n- binary (`Binary`)\n- hexadecimal (`Hex`)\n- BASE-64 (`Base64`)\n- URL-safe BASE-64 (`Base64Url`)\n\nThe `encode` method, which exists as an extension on `IArray[Byte]`, as well as (directly) on all\ntypes representing byte data. It takes one of these as a type parameter to produce a `String` of\nthat data, encoded with the specified encoding.\n\n#### Algorithms\n\nGastronomy's cryptographic functions are implemented through different algorithms, which are\nrepresented by types. Their names follow the conventions of other Scala types, hence:\n- [`Rsa[B]`](https://en.wikipedia.org/wiki/RSA_(cryptosystem)) for `B` of `1024` or `2048`,\n- [`Dsa[B]`](https://en.wikipedia.org/wiki/RSA_(cryptosystem)) for `B` of `512`, `1024`, `2048` or\n  `3072`,\n- [`Aes[B]`](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard) for `B` of `128`, `192` or\n  `256`,\n- [`Sha1`](https://en.wikipedia.org/wiki/SHA-1),\n- [`Sha2[B]`](https://en.wikipedia.org/wiki/SHA-2) for `B` of `224`, `256`, `384` or `512`, and\n- [`Md5`](https://en.wikipedia.org/wiki/MD5)\n\nAdditionally, the types `Base64`, `Base64Url`, `Hex` and `Binary` represent non-cryptographic\nbyte-to-string encodings.\n\n### Generating keys\n\nThe `PrivateKey` object provides the `generate[A]()` method, where `A` is `Rsa[B]`, `Dsa[B]` or\n`Aes[B]` for an appropriate choice of `B`.\n\nThe algorithm `Aes[B]` can also be used with the `SymmetricKey` object to get a symmetric key which\nhas the functionality of both a public and private key.\n\n### Byte codecs\n\nAny object which can be serialized to bytes may be digested, signed, verified, HMACked or encrypted,\nand can be returned from a decryption operation, provided a corresponding `ByteCodec` instance is\navailable for that type.\n\n`ByteCodec`s are provided for `IArray[Byte]` (trivially) and for `String`s (assuming a UTF-8\nencoding).\n\n### PEM encoding\n\nThe _Privacy-Enhanced Mail_ format is commonly used for exchanging cryptographic values safely in\nASCII-only environments.\n\nA `Pem` type is provided for reading, writing and representing this data. The case class `Pem` has\ntwo fields: `kind`, which is the label that appears after the words `BEGIN` and `END` in the\nserialized format, and `data`, which is an `IArray[Byte]` of the byte data.\n\nThe `serialize` method will produce a `String` of the data, properly encoded as BASE-64, and\ndelimited.\n\nThe method `Pem.parse` will attempt to parse a `String` containing PEM-encoded data.\n\nAll Gastronomy's key types offer a `pem` method which will return an appropriately-labelled `Pem`\nvalue containing that key, however to avoid the risk of accidentally exposing a private key, the\n`pem` method of `PrivateKey` must be called with a special singleton value, like so:\n\n```scala\nprivateKey.pem(RevealSecretKey)\n```\n\n### Other Cryptographic Algorithms\n\nGastronomy may be easily extended to support other cryptographic algorithms. The existing\nimplementations of `Rsa`, `Dsa`, `Aes`, `Sha1`, `Sha2` and `Md5` should be studied to investigate\nthis possibility.\n\n\n## Status\n\nGastronomy is classified as __fledgling__. For reference, Soundness projects are\ncategorized into one of the following five stability levels:\n\n- _embryonic_: for experimental or demonstrative purposes only, without any guarantees of longevity\n- _fledgling_: of proven utility, seeking contributions, but liable to significant redesigns\n- _maturescent_: major design decisions broady settled, seeking probatory adoption and refinement\n- _dependable_: production-ready, subject to controlled ongoing maintenance and enhancement; tagged as version `1.0.0` or later\n- _adamantine_: proven, reliable and production-ready, with no further breaking changes ever anticipated\n\nProjects at any stability level, even _embryonic_ projects, can still be used,\nas long as caution is taken to avoid a mismatch between the project's stability\nlevel and the required stability and maintainability of your own project.\n\nGastronomy is designed to be _small_. Its entire source code currently consists\nof 276 lines of code.\n\n## Building\n\nGastronomy will ultimately be built by Fury, when it is published. In the\nmeantime, two possibilities are offered, however they are acknowledged to be\nfragile, inadequately tested, and unsuitable for anything more than\nexperimentation. They are provided only for the necessity of providing _some_\nanswer to the question, \"how can I try Gastronomy?\".\n\n1. *Copy the sources into your own project*\n   \n   Read the `fury` file in the repository root to understand Gastronomy's build\n   structure, dependencies and source location; the file format should be short\n   and quite intuitive. Copy the sources into a source directory in your own\n   project, then repeat (recursively) for each of the dependencies.\n\n   The sources are compiled against the latest nightly release of Scala 3.\n   There should be no problem to compile the project together with all of its\n   dependencies in a single compilation.\n\n2. *Build with [Wrath](https://github.com/propensive/wrath/)*\n\n   Wrath is a bootstrapping script for building Gastronomy and other projects in\n   the absence of a fully-featured build tool. It is designed to read the `fury`\n   file in the project directory, and produce a collection of JAR files which can\n   be added to a classpath, by compiling the project and all of its dependencies,\n   including the Scala compiler itself.\n   \n   Download the latest version of\n   [`wrath`](https://github.com/propensive/wrath/releases/latest), make it\n   executable, and add it to your path, for example by copying it to\n   `/usr/local/bin/`.\n\n   Clone this repository inside an empty directory, so that the build can\n   safely make clones of repositories it depends on as _peers_ of `gastronomy`.\n   Run `wrath -F` in the repository root. This will download and compile the\n   latest version of Scala, as well as all of Gastronomy's dependencies.\n\n   If the build was successful, the compiled JAR files can be found in the\n   `.wrath/dist` directory.\n\n## Contributing\n\nContributors to Gastronomy are welcome and encouraged. New contributors may like\nto look for issues marked\n[beginner](https://github.com/propensive/gastronomy/labels/beginner).\n\nWe suggest that all contributors read the [Contributing\nGuide](/contributing.md) to make the process of contributing to Gastronomy\neasier.\n\nPlease __do not__ contact project maintainers privately with questions unless\nthere is a good reason to keep them private. While it can be tempting to\nrepsond to such questions, private answers cannot be shared with a wider\naudience, and it can result in duplication of effort.\n\n## Author\n\nGastronomy was designed and developed by Jon Pretty, and commercial support and\ntraining on all aspects of Scala 3 is available from [Propensive\nO\u0026Uuml;](https://propensive.com/).\n\n\n\n## Name\n\nGastronomy is named after the art and science of \"good eating\", which leads to digestion, since the library consumes data to produce digests (but has subsequently grown in scope).\n\nIn general, Soundness project names are always chosen with some rationale,\nhowever it is usually frivolous. Each name is chosen for more for its\n_uniqueness_ and _intrigue_ than its concision or catchiness, and there is no\nbias towards names with positive or \"nice\" meanings—since many of the libraries\nperform some quite unpleasant tasks.\n\nNames should be English words, though many are obscure or archaic, and it\nshould be noted how willingly English adopts foreign words. Names are generally\nof Greek or Latin origin, and have often arrived in English via a romance\nlanguage.\n\n## Logo\n\nThe logo shows a slice of avocado, a gastronomic delight.\n\n## License\n\nGastronomy is copyright \u0026copy; 2025 Jon Pretty \u0026 Propensive O\u0026Uuml;, and\nis made available under the [Apache 2.0 License](/license.md).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpropensive%2Fgastronomy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpropensive%2Fgastronomy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpropensive%2Fgastronomy/lists"}