PHP blockchain store data
constructor(index,timestamp,data,previousHash=' 39;)< br />{
this.index=index;< br />this.timestamp=timestamp;< br />this.data=data;< br />this.previousHash=previousHash;< br />$this.hash=this.calculateHash();< br />}
calculateHash(){
return SHA256(this,index+this.previousHash+this.timestamp+JSON.stringify(this.data)).tostring();< br />}
}
class Blockchain{
constructor(){
this.chain=[this.createGenesisBlock()];<
}
/ / create a generated information block
creategenesis block() {
return new block (0, & quot; 01/01/2019",& quot; Genesis block", 0);
/ / the date is the user's data. In fact, it is the hash value obtained by adding several values together. Here, the information is encrypted and the picture is encrypted. Then the data is still stored in the database, but all the data are hash values. To get this data, you must know the hash value of the previous data block. Then, if the hacker needs to crack all the nodes, the hash value is decrypted, Then all the blocks from the first node to the last node can be decrypted to get the real data. So the security of data stored in the blockchain depends on whether the data is encrypted. If the plaintext is not encrypted, then it is not a blockchain
}
/ / get the last block
getlatesblock() {
return this.chain [this.chain. Length-1]
}
/ / create block
addblock (newblock) {
newblock. Previoushash = this. Getlatesblock(). Hash< br />newBlock.hash=newBlock.calculateHash();< br />this.chain.push(newBlock);< br />}
}
on the other hand, all nodes (computers in short) participating in the network will store one account book, which is updated in real time. Even if one node fails, the blockchain will not be affected.
generally, the blockchain platform will provide corresponding interfaces, such as RPC, json-rpc, HTTP, etc. of course, the platforms are not the same, and the degree of friendliness is different
some companies specializing in API, such as blockcypher, can provide friendly calling interfaces, and it is not very convenient to write answers on the mobile phone, Search below
as for the storage content, add a point: in addition to the conventional cloud storage, file entities on the file hash record chain also have blockchain based storage schemes, such as IPFs, storej and so on
Yes
use PHP code to define blocks:
class block {
public $prevhash
public $hash;
public $timeStamp;
public $data;
}
prevhash: the hash value of the previous block; Hash: hash value of the current block; Timestamp: the time stamp generated by the block; Data: data stored in the block; The fields prevhash, hash and timestamp are called block headers in the blockchain, and the hash value of the block is calculated by SHA-256 algorithm
extended data
the practical use and significance of blockchain:
blockchain is a decentralized distributed ledger. What is decentralization? There is no center, or everyone can be a center. This is different from the traditional way of centralization. Distributed ledger means that the storage of data is not only on each node, but each node will and share the data of the whole ledger
In addition, blockchain has the characteristics of disintermediation and information transparency. For example, for example, when we shop on Taobao, after we place the order, the cost we pay is first hit in Alipay. After we receive the courier and confirm the receipt, the money will be transferred to the seller's account. In the blockchain, buyers and sellers can trade directly without any platform as a third-party certification authoritythe system will broadcast the transaction information of both parties. All hosts will record and back up the transaction data after receiving the information. If the order of one machine is wrong, the backup data of other machines will not be affected
the concept of blockchain is a new application mode of distributed data storage, point-to-point transmission, consensus mechanism, encryption algorithm and other computer technologies. Since it is a mode, it is not limited to language, only that language is more suitable
blockchain needs a lot of storage, calculation and transmission. PHP is not suitable for these characteristics, especially the efficiency of PHP
if you want to study this, look at the article of blockchain more:
understanding of blockchain,
51 lines of code to realize a simple PHP blockchain