-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
125 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule ion-hash-test
updated
from 000000 to c2e365
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,7 @@ | ||
# `ion-hash-rust` | ||
|
||
[![Crate](https://img.shields.io/crates/v/ion-hash.svg)](https://crates.io/crates/ion-hash) | ||
[![Docs](https://docs.rs/ion-hash/badge.svg)](https://docs.rs/ion-hash) | ||
[![License](https://img.shields.io/crates/l/ion-hash)](https://crates.io/crates/ion-hash) | ||
[![CI Build](https://github.com/amazon-ion/ion-rust/workflows/CI%20Build/badge.svg)](https://github.com/amazon-ion/ion-rust/actions?query=workflow%3A%22CI+Build%22) | ||
***This package is deprecated in favor of the `ion-hash` feature in `ion-rs >= 0.16.0`.*** | ||
|
||
A Rust implementation of [Ion Hash][spec]. | ||
|
||
***This package is considered experimental, under active/early development, and the API is subject to change.*** | ||
|
||
## Development | ||
|
||
See [Ion Rust][ion-rust] for details. This crate is currently developed in concert with Ion Rust | ||
as a [Cargo workspace][cargo-workspace]. | ||
See [Ion Rust][ion-rust] for details. | ||
|
||
[ion-rust]: https://github.com/amazon-ion/ion-rust | ||
[spec]: https://amazon-ion.github.io/ion-hash/docs/spec.html | ||
[cargo-workspace]: https://doc.rust-lang.org/cargo/reference/workspaces.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,3 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. | ||
|
||
//! Implements the [Ion Hash specification](https://amazon-ion.github.io/ion-hash/docs/spec.html) | ||
//! | ||
//! ## Examples | ||
//! ```rust | ||
//! use ion_rs::value::reader::{self, ElementReader}; | ||
//! use ion_rs::result::IonResult; | ||
//! | ||
//! # fn main() -> IonResult<()> { | ||
//! let loader = reader::element_reader(); | ||
//! let elem = loader.iterate_over(b"\"hello world\"")?.next().unwrap()?; | ||
//! let digest = ion_hash::sha256(&elem); | ||
//! println!("{:?}", digest); | ||
//! # Ok(()) | ||
//! # } | ||
//! ``` | ||
use digest::{FixedOutput, Output, Reset, Update}; | ||
use ion_rs::result::IonResult; | ||
|
||
// TODO: Make sha2 an optional dependency. | ||
use sha2::Sha256; | ||
|
||
use element_hasher::ElementHasher; | ||
use ion_rs::value::owned::Element; | ||
|
||
mod element_hasher; | ||
mod representation; | ||
mod type_qualifier; | ||
|
||
/// Utility to hash an [`Element`] using SHA-256 as the hash function. | ||
// TODO: Make this conditional on some feature flag | ||
pub fn sha256(elem: &Element) -> IonResult<Output<Sha256>> { | ||
Sha256::hash_element(elem) | ||
} | ||
|
||
/// Bytes markers as per the spec. | ||
struct Markers; | ||
impl Markers { | ||
/// single byte begin marker | ||
const B: u8 = 0x0B; | ||
/// single byte end marker | ||
const E: u8 = 0x0E; | ||
/// single byte escape | ||
const ESC: u8 = 0x0C; | ||
} | ||
|
||
/// This trait mostly exists to extend the [`Digest`] trait to support Ion Hash. | ||
/// See [`hash_element`]. | ||
pub trait IonHasher { | ||
type Output; | ||
|
||
/// Returns the Ion Hash of the given [`Element`]. | ||
fn hash_element(elem: &Element) -> IonResult<Self::Output>; | ||
} | ||
|
||
/// Implements [`IonHasher`] for any type that implements `Digest`. | ||
/// | ||
/// Note: the `Digest` trait is implemented for types that implement a set of | ||
/// traits. You should read the `D` generic as `Digest`. The reason we list the | ||
/// subtraits here is that implementors have much less work to do if they | ||
/// implement the subtraits only, as the blanket impls in the `digest` crate take | ||
/// care of assembly. | ||
impl<D> IonHasher for D | ||
where | ||
D: Update + FixedOutput + Reset + Clone + Default, | ||
{ | ||
type Output = digest::Output<D>; | ||
|
||
/// Provides Ion hash over arbitrary [`IonElement`] instances with a given | ||
/// [`Digest`] algorithm. | ||
fn hash_element(elem: &Element) -> IonResult<Self::Output> { | ||
ElementHasher::new(D::default()).hash_element(elem) | ||
} | ||
} | ||
// Intentionally empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. | ||
|
||
//! Implements the [Ion Hash specification](https://amazon-ion.github.io/ion-hash/docs/spec.html) | ||
//! | ||
//! ## Examples | ||
//! ```rust | ||
//! use ion_rs::value::reader::{self, ElementReader}; | ||
//! use ion_rs::result::IonResult; | ||
//! use ion_rs::ion_hash; | ||
//! | ||
//! # fn main() -> IonResult<()> { | ||
//! let loader = reader::element_reader(); | ||
//! let elem = loader.iterate_over(b"\"hello world\"")?.next().unwrap()?; | ||
//! let digest = ion_hash::sha256(&elem); | ||
//! println!("{:?}", digest); | ||
//! # Ok(()) | ||
//! # } | ||
//! ``` | ||
use digest::{self, FixedOutput, Output, Reset, Update}; | ||
|
||
use crate::value::owned::Element; | ||
use crate::IonResult; | ||
use element_hasher::ElementHasher; | ||
|
||
mod element_hasher; | ||
mod representation; | ||
mod type_qualifier; | ||
|
||
#[cfg(feature = "sha2")] | ||
use sha2::Sha256; | ||
/// Utility to hash an [`Element`] using SHA-256 as the hash function. | ||
#[cfg(feature = "sha2")] | ||
pub fn sha256(elem: &Element) -> IonResult<Output<Sha256>> { | ||
Sha256::hash_element(elem) | ||
} | ||
|
||
/// Bytes markers as per the spec. | ||
struct Markers; | ||
impl Markers { | ||
/// single byte begin marker | ||
const B: u8 = 0x0B; | ||
/// single byte end marker | ||
const E: u8 = 0x0E; | ||
/// single byte escape | ||
const ESC: u8 = 0x0C; | ||
} | ||
|
||
/// This trait mostly exists to extend the [`Digest`] trait to support Ion Hash. | ||
/// See [`hash_element`]. | ||
pub trait IonHasher { | ||
type Output; | ||
|
||
/// Returns the Ion Hash of the given [`Element`]. | ||
fn hash_element(elem: &Element) -> IonResult<Self::Output>; | ||
} | ||
|
||
/// Implements [`IonHasher`] for any type that implements [Digest]. | ||
/// | ||
/// Note: the `Digest` trait is implemented for types that implement a set of | ||
/// traits. You should read the `D` generic as `Digest`. The reason we list the | ||
/// subtraits here is that implementors have much less work to do if they | ||
/// implement the subtraits only, as the blanket impls in the `digest` crate take | ||
/// care of assembly. | ||
impl<D> IonHasher for D | ||
where | ||
D: Update + FixedOutput + Reset + Clone + Default, | ||
{ | ||
type Output = digest::Output<D>; | ||
|
||
/// Provides Ion hash over arbitrary [`IonElement`] instances with a given | ||
/// [`Digest`] algorithm. | ||
fn hash_element(elem: &Element) -> IonResult<Self::Output> { | ||
ElementHasher::new(D::default()).hash_element(elem) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters