hash.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #[cfg(target_arch = "bpf")]
  70. pub const fn new_from_array(hash_array: [u8; HASH_BYTES]) -> Self {
  71. Self(hash_array)
  72. }
  73. pub fn to_bytes(self) -> [u8; HASH_BYTES] {
  74. self.0
  75. }
  76. }
  77. /// Return a Sha256 hash for the given data.
  78. pub fn hashv(vals: &[&[u8]]) -> Hash {
  79. // Perform the calculation inline, calling this from within a program is
  80. // not supported
  81. #[cfg(not(target_arch = "bpf"))]
  82. {
  83. let mut hasher = Hasher::default();
  84. hasher.hashv(vals);
  85. hasher.result()
  86. }
  87. // Call via a system call to perform the calculation
  88. #[cfg(target_arch = "bpf")]
  89. {
  90. extern "C" {
  91. fn sol_sha256(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64;
  92. }
  93. let mut hash_result = [0; HASH_BYTES];
  94. unsafe {
  95. sol_sha256(
  96. vals as *const _ as *const u8,
  97. vals.len() as u64,
  98. &mut hash_result as *mut _ as *mut u8,
  99. );
  100. }
  101. Hash::new_from_array(hash_result)
  102. }
  103. }
  104. /// Return a Sha256 hash for the given data.
  105. pub fn hash(val: &[u8]) -> Hash {
  106. hashv(&[val])
  107. }