{"id":23448497,"url":"https://github.com/rggh/cibo","last_synced_at":"2025-04-10T02:35:42.993Z","repository":{"id":268873030,"uuid":"905714943","full_name":"RGGH/cibo","owner":"RGGH","description":"CBOR Serializer and SHA-256 Hasher","archived":false,"fork":false,"pushed_at":"2024-12-19T12:06:29.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-15T18:05:38.466Z","etag":null,"topics":["cibor","rust","sha256"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/RGGH.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"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":"2024-12-19T11:31:40.000Z","updated_at":"2024-12-19T12:06:32.000Z","dependencies_parsed_at":"2024-12-19T12:37:27.523Z","dependency_job_id":"816eb931-5c0b-48c3-867d-f3b2cd040a37","html_url":"https://github.com/RGGH/cibo","commit_stats":null,"previous_names":["rggh/cibo"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RGGH%2Fcibo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RGGH%2Fcibo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RGGH%2Fcibo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RGGH%2Fcibo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RGGH","download_url":"https://codeload.github.com/RGGH/cibo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248144506,"owners_count":21054940,"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":["cibor","rust","sha256"],"created_at":"2024-12-23T22:16:41.345Z","updated_at":"2025-04-10T02:35:42.964Z","avatar_url":"https://github.com/RGGH.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rust CBOR Serializer and SHA-256 Hasher\n\n### Serialize data (such as transaction data or any other structured data) and then compute a SHA-256 hash of that serialized data\n\nThis repository provides a Rust implementation for serializing any `serde::Serialize` type into CBOR format using `ciborium` and hashing the serialized data using SHA-256. The resulting hash is displayed in hexadecimal format.\n\n## Features\n- Serialize any data structure that implements the `serde::Serialize` trait.\n- Use CBOR (Concise Binary Object Representation) for serialization.\n- Compute a SHA-256 hash of the serialized data.\n- Output the hash in hexadecimal format.\n\n---\n\n## Code Explanation\n\n### `Hash` Struct\n```rust\n#[derive(Debug)]\npub struct Hash(U256);\n\nimpl Hash {\n    pub fn to_hex(\u0026self) -\u003e String {\n        // Convert the U256 value to bytes and encode as hexadecimal\n        let mut bytes = [0u8; 32];\n        self.0.to_big_endian(\u0026mut bytes);\n        hex::encode(bytes)\n    }\n}\n```\nThe `Hash` struct encapsulates the hash value using `U256` from the `primitive_types` crate. The `to_hex` method converts the internal hash to a hexadecimal string for easy readability.\n\n### `hash` Function\n```rust\npub fn hash\u003cT: Serialize\u003e(data: \u0026T) -\u003e Hash {\n    let mut serialized: Vec\u003cu8\u003e = vec![];\n    if let Err(e) = ciborium::into_writer(data, \u0026mut serialized) {\n        panic!(\n            \"Failed to serialize data: {:?}. \\\n            This should not happen\",\n            e\n        );\n    }\n\n    let mut hasher = Sha256::new();\n    hasher.update(\u0026serialized);\n    let hash_bytes = hasher.finalize();\n\n    let hash_array: [u8; 32] = hash_bytes.as_slice().try_into().unwrap();\n    Hash(U256::from(hash_array))\n}\n```\nThis function:\n1. Serializes the input data into CBOR format using `ciborium::into_writer`.\n2. Computes the SHA-256 hash of the serialized data.\n3. Converts the hash into a `U256` for storage in the `Hash` struct.\n\n### Example Usage\n```rust\n#[derive(Serialize, Debug)]\nstruct MyData {\n    id: u32,\n    name: String,\n}\n\nfn main() {\n    let data = MyData {\n        id: 42,\n        name: \"Alice\".to_string(),\n    };\n\n    let hashed = hash(\u0026data);\n    println!(\"Hash (hex): {}\", hashed.to_hex());\n}\n```\nThis example demonstrates how to:\n- Define a `serde::Serialize` struct (`MyData`).\n- Serialize and hash an instance of the struct.\n- Print the resulting hash in hexadecimal format.\n\n---\n\n## Prerequisites\n\nEnsure you have Rust installed. If not, install it via [rustup](https://rustup.rs/).\n\n---\n\n## Getting Started\n\n1. Clone the repository:\n   ```bash\n   git clone https://github.com/your-username/sha256-cbor-hasher.git\n   cd sha256-cbor-hasher\n   ```\n\n2. Add dependencies to your `Cargo.toml`:\n   ```toml\n   [dependencies]\n   ciborium = \"0.2\"\n   sha2 = \"0.10\"\n   serde = { version = \"1.0\", features = [\"derive\"] }\n   primitive-types = \"0.10\"\n   hex = \"0.4\"\n   ```\n\n3. Build and run the project:\n   ```bash\n   cargo run\n   ```\n\n---\n\n## Example Output\nFor the input data:\n```rust\nMyData {\n    id: 42,\n    name: \"Alice\".to_string(),\n}\n```\nThe output might look like:\n```\nHash (hex): c9a2e8e9d15b45cf8dbf5c05fa0e30fca0a64d9b0c8d927cb7d2a253b4a2dc3b\n```\n\n---\n\n## License\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n\n---\n\n## Contributions\nContributions are welcome! Feel free to submit a pull request or open an issue to suggest improvements or report bugs.\n\n---\n\n## References\n- [ciborium](https://docs.rs/ciborium)\n- [sha2](https://docs.rs/sha2)\n- [serde](https://docs.rs/serde)\n- [primitive-types](https://docs.rs/primitive-types)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frggh%2Fcibo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frggh%2Fcibo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frggh%2Fcibo/lists"}