Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sha256 for ByteRecord #626

Merged
merged 2 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pica-record/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* #625 Implement `Hash` for `ByteRecord`
* #626 Implement `sha256` for `ByteRecord`

## v0.1.0

Expand Down
1 change: 1 addition & 0 deletions pica-record/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = "2021"
bstr = "1.5"
flate2 = "1.0"
nom = "7.1"
sha2 = "0.10"
thiserror = "1.0"

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions pica-record/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
003@ 0123456789X
38 changes: 33 additions & 5 deletions pica-record/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use nom::combinator::all_consuming;
use nom::multi::many1;
use nom::sequence::terminated;
use nom::Finish;
use sha2::{Digest, Sha256};

use crate::field::{parse_field, RawField};
use crate::parser::{ParseResult, LF};
Expand Down Expand Up @@ -325,6 +326,19 @@ impl<'a> ByteRecord<'a> {
self.record.retain(f);
self.raw_data = None;
}

/// Returns the SHA-256 hash of the record.
pub fn sha256(&self) -> Vec<u8> {
let mut writer = Cursor::new(Vec::<u8>::new());
let mut hasher = Sha256::new();

let _ = self.write_to(&mut writer);
let data = writer.into_inner();
hasher.update(data);

let result = hasher.finalize();
result.to_vec()
}
}

impl<'a> Deref for ByteRecord<'a> {
Expand Down Expand Up @@ -354,12 +368,8 @@ impl<'a> From<RecordRef<'a>> for ByteRecord<'a> {
impl<'a> Hash for ByteRecord<'a> {
fn hash<H: Hasher>(&self, state: &mut H) {
match self.raw_data {
Some(data) => {
eprintln!("hash1");
data.hash(state)
}
Some(data) => data.hash(state),
None => {
eprintln!("hash2");
let mut writer = Cursor::new(Vec::<u8>::new());
let _ = self.write_to(&mut writer);
let data = writer.into_inner();
Expand Down Expand Up @@ -454,6 +464,24 @@ mod tests {
Ok(())
}

#[test]
fn test_byte_record_sha256() -> anyhow::Result<()> {
let record =
ByteRecord::from_bytes(b"003@ \x1f0123456789X\x1e\n")?;

assert_eq!(record.sha256(), b"K\x1f8\xbe\xf4m\xa5\xd0\x8b@{u7\x8bi\x96\x96\xc5\x91\xf6 \xddM\xd3\x8dy\xad[\x96;=\xb6");

let record = ByteRecord::from(RecordRef::new(vec![(
"003@",
None,
vec![('0', "123456789X")],
)]));

assert_eq!(record.sha256(), b"K\x1f8\xbe\xf4m\xa5\xd0\x8b@{u7\x8bi\x96\x96\xc5\x91\xf6 \xddM\xd3\x8dy\xad[\x96;=\xb6");

Ok(())
}

#[test]
fn test_string_record() -> anyhow::Result<()> {
let record =
Expand Down