hash.rs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Utility hashing module copied from `solana_program::program::hash`, since we
  2. // can't import solana_program for compile time hashing for some reason.
  3. use serde::{Deserialize, Serialize};
  4. use sha2::{Digest, Sha256};
  5. use std::{fmt, mem, str::FromStr};
  6. use thiserror::Error;
  7. pub const HASH_BYTES: usize = 32;
  8. #[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
  9. #[repr(transparent)]
  10. pub struct Hash(pub [u8; HASH_BYTES]);
  11. #[derive(Clone, Default)]
  12. pub struct Hasher {
  13. hasher: Sha256,
  14. }
  15. impl Hasher {
  16. pub fn hash(&mut self, val: &[u8]) {
  17. self.hasher.update(val);
  18. }
  19. pub fn hashv(&mut self, vals: &[&[u8]]) {
  20. for val in vals {
  21. self.hash(val);
  22. }
  23. }
  24. pub fn result(self) -> Hash {
  25. // At the time of this writing, the sha2 library is stuck on an old version
  26. // of generic_array (0.9.0). Decouple ourselves with a clone to our version.
  27. Hash(<[u8; HASH_BYTES]>::try_from(self.hasher.finalize().as_slice()).unwrap())
  28. }
  29. }
  30. impl AsRef<[u8]> for Hash {
  31. fn as_ref(&self) -> &[u8] {
  32. &self.0[..]
  33. }
  34. }
  35. impl fmt::Debug for Hash {
  36. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  37. write!(f, "{}", bs58::encode(self.0).into_string())
  38. }
  39. }
  40. impl fmt::Display for Hash {
  41. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  42. write!(f, "{}", bs58::encode(self.0).into_string())
  43. }
  44. }
  45. #[derive(Debug, Clone, PartialEq, Eq, Error)]
  46. pub enum ParseHashError {
  47. #[error("string decoded to wrong size for hash")]
  48. WrongSize,
  49. #[error("failed to decoded string to hash")]
  50. Invalid,
  51. }
  52. impl FromStr for Hash {
  53. type Err = ParseHashError;
  54. fn from_str(s: &str) -> Result<Self, Self::Err> {
  55. let bytes = bs58::decode(s)
  56. .into_vec()
  57. .map_err(|_| ParseHashError::Invalid)?;
  58. if bytes.len() != mem::size_of::<Hash>() {
  59. Err(ParseHashError::WrongSize)
  60. } else {
  61. Ok(Hash::new(&bytes))
  62. }
  63. }
  64. }
  65. impl Hash {
  66. pub fn new(hash_slice: &[u8]) -> Self {
  67. Hash(<[u8; HASH_BYTES]>::try_from(hash_slice).unwrap())
  68. }
  69. pub fn to_bytes(self) -> [u8; HASH_BYTES] {
  70. self.0
  71. }
  72. }
  73. /// Return a Sha256 hash for the given data.
  74. pub fn hashv(vals: &[&[u8]]) -> Hash {
  75. // Perform the calculation inline, calling this from within a program is
  76. // not supported
  77. let mut hasher = Hasher::default();
  78. hasher.hashv(vals);
  79. hasher.result()
  80. }
  81. /// Return a Sha256 hash for the given data.
  82. pub fn hash(val: &[u8]) -> Hash {
  83. hashv(&[val])
  84. }