sha256.rs
1 // Copyright (c) 2025 ADnet Contributors 2 // This file is part of the AlphaVM library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use sha2::{Digest, Sha256}; 17 18 pub fn sha256(data: &[u8]) -> [u8; 32] { 19 let digest = Sha256::digest(data); 20 let mut ret = [0u8; 32]; 21 ret.copy_from_slice(&digest); 22 ret 23 } 24 25 pub fn double_sha256(data: &[u8]) -> [u8; 32] { 26 let digest = Sha256::digest(Sha256::digest(data)); 27 let mut ret = [0u8; 32]; 28 ret.copy_from_slice(&digest); 29 ret 30 } 31 32 pub fn sha256d_to_u64(data: &[u8]) -> u64 { 33 let hash_slice = double_sha256(data); 34 let mut hash = [0u8; 8]; 35 hash[..].copy_from_slice(&hash_slice[..8]); 36 u64::from_le_bytes(hash) 37 }