-
Notifications
You must be signed in to change notification settings - Fork 2
/
025-cryptographic-functions.sol
66 lines (55 loc) · 2.14 KB
/
025-cryptographic-functions.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* Cryptographic Functions
* A cryptographic has function(CHF) is a mathematical algorithm that maps data of arbitrary size
* (often called the "message") to a bit array of a fixed size (called the "hash" or "hash value" or "message digest")
* It is a one way function, that is, a function which is practically infeasible to invert or reverse the computation.
*
* Solidity provides inbuilt cryptographic functions as well. Below are some of the important methods it provides:
*
* keccak256(bytes memory) returns (byte32)
* sha256(bytes memory) returns (bytes 32)
* ripemd160(bytes memory) returns (bytes 20)
*/
// Keccak is a leading hashing function, designed by non-NSA designers. Keccak won NIST competition to become the official SHA3
// Keccak is a family of cryptographic sponge functions and is designed as an alternative to SHA-256
// Oracle dynamic feeds
// A database management system is not only used for storing the data but to effectively manage
// it and provides high performance, authorized access and failure recovery features.
contract Oracle {
address admin;
uint256 public rand;
constructor() {
admin = msg.sender;
}
// seed the rand variable
function seedRandom(uint256 _rand) external {
require(msg.sender == admin);
rand = _rand;
}
}
contract LearnCryptoGraphicFunctions {
// we will leaverage this module on two things
// 1. Modulo operator
// 2. Cryptographic hashing
Oracle oracle;
constructor(address oracleAddress) {
oracle = Oracle(oracleAddress);
}
function randMod(uint256 range) external view returns (uint256) {
// grab information from the blockchain to randomly generate numbers
// abi.encodePacked concatenates arguments nicely
return
uint256(
keccak256(
abi.encodePacked(
oracle.rand,
block.timestamp,
block.difficulty,
msg.sender
)
)
) % range;
}
}