hash.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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::{convert::TryFrom, 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. #[cfg(not(target_arch = "bpf"))]
  78. {
  79. let mut hasher = Hasher::default();
  80. hasher.hashv(vals);
  81. hasher.result()
  82. }
  83. // Call via a system call to perform the calculation
  84. #[cfg(target_arch = "bpf")]
  85. {
  86. extern "C" {
  87. fn sol_sha256(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64;
  88. };
  89. let mut hash_result = [0; HASH_BYTES];
  90. unsafe {
  91. sol_sha256(
  92. vals as *const _ as *const u8,
  93. vals.len() as u64,
  94. &mut hash_result as *mut _ as *mut u8,
  95. );
  96. }
  97. Hash::new_from_array(hash_result)
  98. }
  99. }
  100. /// Return a Sha256 hash for the given data.
  101. pub fn hash(val: &[u8]) -> Hash {
  102. hashv(&[val])
  103. }