Solidity-023 CryptoFunctions

发布时间:2024年01月23日

// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

contract CryptoFunctions {

? ?

? ? // cryptoDemo function: Demonstrates the use of two cryptographic hash functions - SHA-256 and Keccak256.

? ? // It takes no inputs and returns two hash values, one for each hash function, both generated from the same string "r".

? ? function cryptoDemo() public pure returns (bytes32, bytes32){

? ? ? ? // SHA-256 hash function applied to the string "r"

? ? ? ? bytes32 sha256Hash = sha256("r");

? ? ? ?

? ? ? ? // Keccak256 hash function (the same hashing algorithm used by Ethereum) applied to the string "r"

? ? ? ? bytes32 keccak256Hash = keccak256("r");

? ? ? ? // Returning both hashes

? ? ? ? return (sha256Hash, keccak256Hash);

? ? }

? ?

? ? // ripemd160Hash function: Computes the RIPEMD-160 hash of a given input string.

? ? // This function takes a string as input and returns its RIPEMD-160 hash.

? ? function ripemd160Hash(string memory _input) public pure returns (bytes20) {

? ? ? ? // Encoding the string to bytes and applying the RIPEMD-160 hash function

? ? ? ? return ripemd160(abi.encodePacked(_input));

? ? }

? ? // concatenateStrings function: Concatenates two input strings into one.

? ? // This function takes two strings as inputs and returns their concatenation.

? ? function concatenateStrings(string memory _str1, string memory _str2) public pure returns (string memory) {

? ? ? ? // Using abi.encodePacked to efficiently concatenate the strings and then converting the result back to a string

? ? ? ? return string(abi.encodePacked(_str1, _str2));

? ? }

}

//Deploy screenshot:

文章来源:https://blog.csdn.net/m0_61408373/article/details/135783640
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。