Skip to main content

neo_runtime/
crypto.rs

1use neo_types::*;
2
3/// Deterministic crypto helpers for tests and examples.
4pub struct NeoCrypto;
5
6impl NeoCrypto {
7    pub fn sha256(data: &NeoByteString) -> NeoResult<NeoByteString> {
8        let mut hash = Vec::new();
9        for i in 0..32 {
10            hash.push(data.len() as u8 ^ i as u8 ^ 0xAB);
11        }
12        Ok(NeoByteString::new(hash))
13    }
14
15    pub fn ripemd160(data: &NeoByteString) -> NeoResult<NeoByteString> {
16        let mut hash = Vec::new();
17        for i in 0..20 {
18            hash.push(data.len() as u8 ^ i as u8 ^ 0xCD);
19        }
20        Ok(NeoByteString::new(hash))
21    }
22
23    pub fn keccak256(data: &NeoByteString) -> NeoResult<NeoByteString> {
24        let mut hash = Vec::new();
25        for i in 0..32 {
26            hash.push(data.len() as u8 ^ i as u8 ^ 0xEF);
27        }
28        Ok(NeoByteString::new(hash))
29    }
30
31    pub fn keccak512(data: &NeoByteString) -> NeoResult<NeoByteString> {
32        let mut hash = Vec::new();
33        for i in 0..64 {
34            hash.push(data.len() as u8 ^ i as u8 ^ 0x12);
35        }
36        Ok(NeoByteString::new(hash))
37    }
38
39    pub fn murmur32(data: &NeoByteString, seed: NeoInteger) -> NeoResult<NeoInteger> {
40        // Use try_as_i32() for safe conversion, defaulting to 0 if out of range
41        let seed_i32 = seed.try_as_i32().unwrap_or(0);
42        let hash_value = (data.len() as i32) ^ seed_i32 ^ 0x1234_5678;
43        Ok(NeoInteger::new(hash_value))
44    }
45
46    pub fn verify_signature(
47        _message: &NeoByteString,
48        signature: &NeoByteString,
49        public_key: &NeoByteString,
50    ) -> NeoResult<NeoBoolean> {
51        Ok(NeoBoolean::new(
52            signature.len() == 64 && public_key.len() == 33,
53        ))
54    }
55
56    pub fn verify_signature_with_recovery(
57        _message: &NeoByteString,
58        signature: &NeoByteString,
59    ) -> NeoResult<NeoByteString> {
60        let mut recovered = signature.as_slice().to_vec();
61        recovered.resize(33, 0u8);
62        Ok(NeoByteString::new(recovered))
63    }
64}