rustpublic-key-encryptionrust-cratessha-3keccak

no new() in sha3::Keccak256 struct


I'm trying to get an ethereum public address from a hex private key. I wrote a rust script for that -

extern crate hex;
extern crate secp256k1;
extern crate sha3;

use secp256k1::{PublicKey, SecretKey};
use sha3::Keccak256;

fn main() {
    let context = secp256k1::Secp256k1::new();
    let private_key: &[u8] =
        "616E6769652E6A6A706572657A616775696E6167612E6574682E6C696E6B0D0A".as_bytes();
    let secret_key = SecretKey::from_slice(&hex::decode(private_key).unwrap());
    let public_key = PublicKey::from_secret_key(&context, &secret_key.unwrap());
    println!("Public Key -> {:?}", public_key);
    let mut hasher = Keccak256::new();
    // making it an ethereum address
}

But let mut hasher = Keccak256::new(); is giving me an error -

error[E0599]: no function or associated item named `new` found for struct `CoreWrapper` in the current scope
  --> src/main.rs:14:33
   |
14 |     let mut hasher = Keccak256::new();
   |                                 ^^^ function or associated item not found in `CoreWrapper<Keccak256Core>`
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
5  | use crate::sha3::Digest;
   |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `ecdsa-test` due to previous error

Solution

  • Try this solution:

    use sha3::{Digest, Keccak256};
    let mut hasher = Keccak256::new();
    hasher.update(data);
    let result = hasher.finalize();