Skip to content

Commit

Permalink
keccak-hash: add keccak256_range and keccak512_range functions (#370)
Browse files Browse the repository at this point in the history
* keccak-hash: add keccak256_range and keccak512_range functions

* keccak-hash: prep for release

* keccak-hash: update the date in changelog

* keccak-hash: more docs

* Update keccak-hash/src/lib.rs

Co-Authored-By: David <[email protected]>

Co-authored-by: David <[email protected]>
  • Loading branch information
ordian and dvdplm authored Apr 10, 2020
1 parent b87aee1 commit 0424206
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion keccak-hash/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ The format is based on [Keep a Changelog].

## [Unreleased]

## [0.4.2] - 2020-03-16
## [0.5.1] - 2020-04-10
- Added `keccak256_range` and `keccak512_range` functions. [#370](https://github.com/paritytech/parity-common/pull/370)

## [0.5.0] - 2020-03-16
- License changed from GPL3 to dual MIT/Apache2. [#342](https://github.com/paritytech/parity-common/pull/342)
- Updated dependencies. [#361](https://github.com/paritytech/parity-common/pull/361)
- Updated tiny-keccak. [#260](https://github.com/paritytech/parity-common/pull/260)

## [0.4.1] - 2019-10-24
### Dependencies
Expand Down
51 changes: 51 additions & 0 deletions keccak-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,64 @@ pub fn keccak256(data: &mut [u8]) {
keccak256.finalize(data);
}

/// Computes in-place keccak256 hash of `data[range]`.
///
/// The `range` argument specifies a subslice of `data` in bytes to be hashed.
/// The resulting hash will be written back to `data`.
/// # Panics
///
/// If `range` is out of bounds.
///
/// # Example
///
/// ```
/// let mut data = [1u8; 32];
/// // Hash the first 8 bytes of `data` and write the result, 32 bytes, to `data`.
/// keccak_hash::keccak256_range(&mut data, 0..8);
/// let expected = [
/// 0x54, 0x84, 0x4f, 0x69, 0xb4, 0xda, 0x4b, 0xb4, 0xa9, 0x9f, 0x24, 0x59, 0xb5, 0x11, 0xd4, 0x42,
/// 0xcc, 0x5b, 0xd2, 0xfd, 0xf4, 0xc3, 0x54, 0xd2, 0x07, 0xbb, 0x13, 0x08, 0x94, 0x43, 0xaf, 0x68,
/// ];
/// assert_eq!(&data, &expected);
/// ```
pub fn keccak256_range(data: &mut [u8], range: core::ops::Range<usize>) {
let mut keccak256 = Keccak::v256();
keccak256.update(&data[range]);
keccak256.finalize(data);
}

/// Computes in-place keccak512 hash of `data`.
pub fn keccak512(data: &mut [u8]) {
let mut keccak512 = Keccak::v512();
keccak512.update(data.as_ref());
keccak512.finalize(data);
}

/// Computes in-place keccak512 hash of `data[range]`.
///
/// The `range` argument specifies a subslice of `data` in bytes to be hashed.
/// The resulting hash will be written back to `data`.
/// # Panics
///
/// If `range` is out of bounds.
///
/// # Example
///
/// ```
/// let mut data = [1u8; 64];
/// keccak_hash::keccak512_range(&mut data, 0..8);
/// let expected = [
/// 0x90, 0x45, 0xc5, 0x9e, 0xd3, 0x0e, 0x1f, 0x42, 0xac, 0x35, 0xcc, 0xc9, 0x55, 0x7c, 0x77, 0x17,
/// 0xc8, 0x89, 0x3a, 0x77, 0x6c, 0xea, 0x2e, 0xf3, 0x88, 0xea, 0xe5, 0xc0, 0xea, 0x40, 0x26, 0x64,
/// ];
/// assert_eq!(&data[..32], &expected);
/// ```
pub fn keccak512_range(data: &mut [u8], range: core::ops::Range<usize>) {
let mut keccak512 = Keccak::v512();
keccak512.update(&data[range]);
keccak512.finalize(data);
}

pub fn keccak_256(input: &[u8], output: &mut [u8]) {
write_keccak(input, output);
}
Expand Down

0 comments on commit 0424206

Please sign in to comment.