Is it possible to perform cryptographic operations inside a smart contract? I'm specifically thinking about simple hashing (SHA-1/256/3) and symmetric encryption (AES). Obviously, handling key material for encryption is an issue that has to be addressed. But is it possible, in general, to do these operations?
5 Answers
There's a crypto.h inside of the contracts/eosiolib
folder that has SHA/ripemd methods.
AES should be done off-chain though since the cleartext would be visible to anyone replaying the blockchain and viewing the action arguments.
Yes! It's very much possible! Look at the Dice contract example on the eosio github or in the repo folder eos/contracts/dice The function shown is a sha256 hashing method called sha256( )
Link to the actual dice.cpp file containing it's use on github: The Eosio example dice.cpp file
-
In the example dice contract, where does the
sha256()
function come from? It isn't defined in the contract. Commented May 9, 2018 at 23:15 -
1@nsjames Seems to have answered your question, crypto.h in contracts/eos from the github repository. You can also check out the API documentation at EOSIO crypto.h API Documentation Commented May 10, 2018 at 4:34
Yes, it is possible. Please see here: https://github.com/EOSIO/eos/blob/master/contracts/eosiolib/crypto.h
Developers can also compile their favourite C/C++ crypto library to do things EOSIO natively doesn't support.
The operations in contracts should be as short as possible. Because it’s directly pertain to the block producing time.
I recommend you to do cryptographic things outside of contract and pass the results as binary to the contract.
-
1base64 is not a good format for contracts to operate on. Contracts should operate on binary. Commented May 10, 2018 at 4:52