-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Switch SHA-256 implementation from libsodium to Rust. #5364
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,58 @@ | ||
| // Copyright 2026 Stellar Development Foundation and contributors. Licensed | ||
| // under the Apache License, Version 2.0. See the COPYING file at the root | ||
| // of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| // SHA-256 implemented via the RustCrypto `sha2` crate, exposed to C++ over the | ||
| // cxx bridge. | ||
|
|
||
| use sha2::{Digest, Sha256}; | ||
|
|
||
| // Computes SHA-256 of `data_len` bytes at `data` and writes the 32-byte digest | ||
| // to `out`. `out` must point to at least 32 writable bytes. | ||
| // | ||
| // # Safety | ||
| // `data`/`data_len` must describe a valid readable region (or len 0), and `out` | ||
| // must point to 32 writable bytes. | ||
| pub(crate) unsafe fn compute_sha256(data: *const u8, data_len: usize, out: *mut u8) { | ||
| let input: &[u8] = if data_len == 0 { | ||
| &[] | ||
| } else { | ||
| std::slice::from_raw_parts(data, data_len) | ||
| }; | ||
| let digest = Sha256::digest(input); | ||
| std::ptr::copy_nonoverlapping(digest.as_ptr(), out, 32); | ||
| } | ||
|
|
||
| // Incremental SHA-256 state, exposed to C++ as an opaque cxx type so that | ||
| // callers can stream bytes in without first materializing a contiguous buffer. | ||
| pub(crate) struct RustSha256 { | ||
| hasher: Sha256, | ||
| } | ||
|
|
||
| pub(crate) fn new_rust_sha256() -> Box<RustSha256> { | ||
| Box::new(RustSha256 { | ||
| hasher: Sha256::new(), | ||
| }) | ||
| } | ||
|
|
||
| impl RustSha256 { | ||
| // # Safety: `data`/`len` must describe a valid readable region (or len 0). | ||
| pub(crate) unsafe fn update(&mut self, data: *const u8, len: usize) { | ||
| let input: &[u8] = if len == 0 { | ||
| &[] | ||
| } else { | ||
| std::slice::from_raw_parts(data, len) | ||
| }; | ||
| self.hasher.update(input); | ||
| } | ||
|
|
||
| // # Safety: `out` must point to 32 writable bytes. Resets the hasher. | ||
| pub(crate) unsafe fn finalize(&mut self, out: *mut u8) { | ||
| let digest = self.hasher.finalize_reset(); | ||
| std::ptr::copy_nonoverlapping(digest.as_ptr(), out, 32); | ||
| } | ||
|
|
||
| pub(crate) fn reset(&mut self) { | ||
| self.hasher.reset(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.