-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dm4
committed
Jul 24, 2018
1 parent
4bff285
commit e5bf765
Showing
1 changed file
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
RSA Crypto | ||
============== | ||
|
||
.. _rsa-crypto: | ||
|
||
This example shows how to use our :code:`eni_crypto.so` to do RSA encrypt / decrypt. | ||
|
||
Install lity branch of Travis | ||
----------------------------- | ||
|
||
First we need to use lity branch of travis: | ||
|
||
.. code:: bash | ||
# Get lity branch of Travis | ||
go get github.com/CyberMiles/travis | ||
cd $GOPATH/src/github.com/CyberMiles/travis | ||
git checkout lity | ||
# Install libeni | ||
wget -O $HOME/libeni.tgz https://github.com/CyberMiles/libeni/releases/download/v1.2.0/libeni-1.2.0_ubuntu-16.04.tgz | ||
tar zxvf $HOME/libeni.tgz -C $HOME | ||
mkdir -p $HOME/.travis/eni | ||
cp -r $HOME/libeni-1.2.0/lib $HOME/.travis/eni/lib | ||
# Build & Install Travis | ||
cd $GOPATH/src/github.com/CyberMiles/travis | ||
make all | ||
Run Travis locally | ||
------------------ | ||
|
||
Then start a Travis node locally. | ||
|
||
.. code:: bash | ||
# Initial & start | ||
travis node init | ||
travis node start | ||
Lity compiler | ||
------------- | ||
|
||
Build our Lity compiler from source to get :code:`lityc`. | ||
|
||
.. code:: bash | ||
git clone https://github.com/CyberMiles/lity.git | ||
cd lity | ||
git submodule update --init --recursive | ||
mkdir build | ||
cd build | ||
cmake .. | ||
make | ||
Now we have :code:`lityc`: | ||
|
||
.. code:: bash | ||
$ ls -l ./lityc/lityc | ||
-rwxrwxr-x 1 skymizer skymizer 26649648 Jul 3 02:19 lityc/lityc* | ||
Compiler RSACrypto Contract | ||
--------------------------- | ||
|
||
There is a simple contract that leverage ENI to do RSA encrypt / decrypt. | ||
|
||
.. code:: | ||
pragma solidity ^0.4.0; | ||
contract RSACrypto { | ||
function encrypt(string pubkey, string plaintext) public pure returns (string) { | ||
string memory ret; | ||
ret = eni("rsa_encrypt", pubkey, plaintext); | ||
return ret; | ||
} | ||
function decrypt(string prikey, string ciphertext) public pure returns (string) { | ||
string memory ret; | ||
ret = eni("rsa_decrypt", prikey, ciphertext); | ||
return ret; | ||
} | ||
} | ||
and we could compile it using :code:`lityc`: | ||
|
||
.. code:: bash | ||
$ mkdir output | ||
$ ./lityc --abi --bin -o output RSACrypto.sol | ||
$ cat output/RSACrypto.abi | ||
[{"constant":true,"inputs":[{"name":"pubkey","type":"string"},{"name":"plaintext","type":"string"}],"name":"encrypt","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"name":"prikey","type":"string"},{"name":"ciphertext","type":"string"}],"name":"decrypt","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"pure","type":"function"}] | ||
$ cat output/RSACrypto.bin | ||
608060405234801561001057600080fd5b506105e1806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063125704b81461005157806325f1a7cf14610179575b600080fd5b34801561005d57600080fd5b506100fe600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506102a1565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820d805160018360200d6101000a0d1916815260200191505b509250505060405180910390f35b34801561018557600080fd5b50610226600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919291929050505061042b565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561026657808201518184015260208101905061024b565b50505050905090810190601f1680156102935780820d805160018360200d6101000a0d1916815260200191505b509250505060405180910390f35b60608060405160206040519081016040526002905260206040519081016040527f0404000000000000000000000000000000000000000000000000000000000000905260206040519081016040526001905260206040519081016040527f04000000000000000000000000000000000000000000000000000000000000009052602060405190810160405280600090528580516020019081601f0160209004602002604051908101604052905b602083101515610373578051825260208201915060208101905060208303925061034e565b6001836020036101000a0d80198251168184511680821785525050505050508480516020019081601f0160209004602002604051908101604052905b6020831015156103d457805182526020820191506020810190506020830392506103af565b6001836020036101000a0d8019825116818451168082178552505050505050806040518190039052907f7273615f656e6372797074000000000000000000000000000000000000000000f590508091505092915050565b60608060405160206040519081016040526002905260206040519081016040527f0404000000000000000000000000000000000000000000000000000000000000905260206040519081016040526001905260206040519081016040527f04000000000000000000000000000000000000000000000000000000000000009052602060405190810160405280600090528580516020019081601f0160209004602002604051908101604052905b6020831015156104fd57805182526020820191506020810190506020830392506104d8565b6001836020036101000a0d80198251168184511680821785525050505050508480516020019081601f0160209004602002604051908101604052905b60208310151561055e5780518252602082019150602081019050602083039250610539565b6001836020036101000a0d8019825116818451168082178552505050505050806040518190039052907f7273615f64656372797074000000000000000000000000000000000000000000f5905080915050929150505600a165627a7a72305820e288ae5fb263ab37cadb62d4eca245c81d2a0f891b9b7ee1b7ab6f5b5f2a9f6c0029 | ||
Deploy contract to Travis locally | ||
--------------------------------- | ||
|
||
After we get contract ABI and bytecode, we could deploy it to Travis chain. | ||
|
||
.. code:: bash | ||
# Get Travis console | ||
travis attach http://127.0.0.1:8545 | ||
# Deploy contract (in Travis console) | ||
personal.unlockAccount(cmt.accounts[0], '1234'); | ||
abi = [{"constant":true,"inputs":[{"name":"pubkey","type":"string"},{"name":"plaintext","type":"string"}],"name":"encrypt","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"name":"prikey","type":"string"},{"name":"ciphertext","type":"string"}],"name":"decrypt","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"pure","type":"function"}] | ||
bytecode = "0x608060405234801561001057600080fd5b506105e1806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063125704b81461005157806325f1a7cf14610179575b600080fd5b34801561005d57600080fd5b506100fe600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506102a1565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820d805160018360200d6101000a0d1916815260200191505b509250505060405180910390f35b34801561018557600080fd5b50610226600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919291929050505061042b565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561026657808201518184015260208101905061024b565b50505050905090810190601f1680156102935780820d805160018360200d6101000a0d1916815260200191505b509250505060405180910390f35b60608060405160206040519081016040526002905260206040519081016040527f0404000000000000000000000000000000000000000000000000000000000000905260206040519081016040526001905260206040519081016040527f04000000000000000000000000000000000000000000000000000000000000009052602060405190810160405280600090528580516020019081601f0160209004602002604051908101604052905b602083101515610373578051825260208201915060208101905060208303925061034e565b6001836020036101000a0d80198251168184511680821785525050505050508480516020019081601f0160209004602002604051908101604052905b6020831015156103d457805182526020820191506020810190506020830392506103af565b6001836020036101000a0d8019825116818451168082178552505050505050806040518190039052907f7273615f656e6372797074000000000000000000000000000000000000000000f590508091505092915050565b60608060405160206040519081016040526002905260206040519081016040527f0404000000000000000000000000000000000000000000000000000000000000905260206040519081016040526001905260206040519081016040527f04000000000000000000000000000000000000000000000000000000000000009052602060405190810160405280600090528580516020019081601f0160209004602002604051908101604052905b6020831015156104fd57805182526020820191506020810190506020830392506104d8565b6001836020036101000a0d80198251168184511680821785525050505050508480516020019081601f0160209004602002604051908101604052905b60208310151561055e5780518252602082019150602081019050602083039250610539565b6001836020036101000a0d8019825116818451168082178552505050505050806040518190039052907f7273615f64656372797074000000000000000000000000000000000000000000f5905080915050929150505600a165627a7a72305820e288ae5fb263ab37cadb62d4eca245c81d2a0f891b9b7ee1b7ab6f5b5f2a9f6c0029" | ||
contract = web3.cmt.contract(abi); | ||
c = contract.new( | ||
{ | ||
from: web3.cmt.accounts[0], | ||
data: bytecode, | ||
gas: "4700000" | ||
}, | ||
function(e, contract) { | ||
if (contract.address) { | ||
console.log("contract address: " + contract.address); | ||
console.log("transactionHash: " + contract.transactionHash); | ||
} | ||
} | ||
); | ||
Use contract to do RSA encrypt / decrypt | ||
---------------------------------------- | ||
.. code:: bash | ||
# Setup private & public keys | ||
prikey = "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEApr/atzUuBArbsWHkn8tUmq00tCV4CcLfVUVg+zr41sixYIb5\n4rd5EFWoQ1xecYMZIbnoTl2vu9awHBZF18DkNlG8pjw1Vw5EjaWrCDn734lcKGhs\nYe20H7138XgznwhJNuAeNy2OOwbEIkSl4jf8Ro+7nOuMK5yeHXAPZEMCnEipd/7g\nGk0aWP/E1XoqkZJnBUYN5N6mOgtV3jv62w+XlNlozUySI0mBmjgyymAhPm4qx5Zc\nf/Wmg42vbIFRrBl5PgWKGsY0L7xcPRDPAPRtndUPr+CLLk5KjyHI8a2WiYrJvjUG\nTQNyqPM5MmLLfHMkjkbE6DshWbMZona+/5ji3wIDAQABAoIBAAjwNdAmSJ4s2tPq\nVHAAXTuhVzbk30deq8wNWQJ+icIxpdhvw8tUXGf0v31E4UciaOF27q3stbPS8UPA\nKeRD0bfbr8oVZiKRgDk7jSx2tzqnSUpdNpoVPNeKt3g5IkM/FXWck+IPThV56l+P\n4Hh82cgKglsKAUyBK7SWQiz0rpoj8MWlkG0TblsMVLnOTAO0N3p3NiHxv1eUJrHK\nwy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MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApr/atzUuBArbsWHkn8tU\nmq00tCV4CcLfVUVg+zr41sixYIb54rd5EFWoQ1xecYMZIbnoTl2vu9awHBZF18Dk\nNlG8pjw1Vw5EjaWrCDn734lcKGhsYe20H7138XgznwhJNuAeNy2OOwbEIkSl4jf8\nRo+7nOuMK5yeHXAPZEMCnEipd/7gGk0aWP/E1XoqkZJnBUYN5N6mOgtV3jv62w+X\nlNlozUySI0mBmjgyymAhPm4qx5Zcf/Wmg42vbIFRrBl5PgWKGsY0L7xcPRDPAPRt\nndUPr+CLLk5KjyHI8a2WiYrJvjUGTQNyqPM5MmLLfHMkjkbE6DshWbMZona+/5ji\n3wIDAQAB\n-----END PUBLIC KEY-----"; | ||
# Encrypt | ||
> ciphertext = c.encrypt.call(pubkey, 'Hello World!') | ||
"49d511a44a3d2a2437f00a72fef6915fd0fcd669e90bd2f62387c6bdd3028ecc5b8799bae0a8bae606655046c87416297b5e8e130359fca8e612ab10f0dfe1a9abdceb3f62b5edf20a4c3925f9129782e7a51020a7252b056afca88a4f087bd5700737accdd2867adcf429818edacd936ffe8afd013893dd0d268f335e3bb3c8f2853dd490a7c52b6eec428138aecf26563ce06ef517d2f643b9cacb1bfda0ade8c530f550c0d4fb170fcdeeebad2f362bb6fb83484754fea03df53e0f06ca4b7c98f91685a293bae854d8a6cde108726468cc3e927eeb5dec6de6d11ca30e59ab81c5019a217453ce984edcea652a91a40d448fd5981b01aececb258e70282a" | ||
# Decrypt | ||
> c.decrypt.call(prikey, ciphertext) | ||
"Hello World!" |