PRACTICAL
FILE
“BLOCK CHAIN TECHNOLOGY”
POST GRADUATE DIPLOMA IN
CYBER SECURITY, CYBER DISASTER
AND BLOCKCHAIN TECHNOLOGY
Trimester Mode
Session 2023-24
University School of Automation and Robotics
GURU GOBIND SINGH INDRAPRASTHA
UNIVERSITY (NAAC A++)
EAST DELHI CAMPUS, SURAJMAL VIHAR-
110032
SUBMITTED BY :- RITIK KUMAR TILWARI SUBMITTED TO :- DR
RAHUL JOHRI
MOB:- 9540064603
ASSOC. PROF
Enrolment no :-01019017723
MAIL ID – ritikkumartilwari@gmail.com
INDEX
S.NO. PROGRAM NAME
1.
To write a Java program using MD 5 Algorithm for generating
a Hash Value for a credit card number input by the user.
2.
To write a Python program using SHA Algorithm on
generating a Hash Value for a credit card number "1634
5678 2345 9812".
3.
To write a Python program using SHA Algorithm on
generating a Hash Value for a string " GGSIPU".
4.
Using REMIX IDE to write a smart contract to generate a
hash value using pair of getter and setter functions.
5.
Using REMIX IDE to write a smart contract to display your
own name.
6.
Using REMIX IDE to write a smart contract to perform the
addition of the two numbers.
7.
Using REMIX IDE write a Smart Contract to generate NFT.
1. To write a Java program using MD 5 Algorithm for generating a Hash Value for a credit
card number input by the user.
Source Code:-
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
public class CreditCardHashGenerator
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get credit card number from the user
System.out.print("Enter the credit card number: ");
String creditCardNumber = scanner.nextLine();
// Generate MD5 hash for the credit card number
String hashValue = generateMD5Hash(creditCardNumber);
// Display the generated hash value
System.out.println("MD5 Hash Value: " + hashValue);
// Close the scanner
scanner.close();
}
private static String generateMD5Hash(String input) {
try {
// Create MessageDigest instance for MD5
MessageDigest md = MessageDigest.getInstance("MD5");
// Add input string bytes to digest
md.update(input.getBytes());
// Get the MD5 hash bytes
byte[] hashBytes = md.digest();
// Convert the bytes to hexadecimal format
StringBuilder hexStringBuilder = new StringBuilder();
for (byte hashByte : hashBytes) {
hexStringBuilder.append(String.format("%02x", hashByte));
}
return hexStringBuilder.toString();
} catch (NoSuchAlgorithmException e) {
// Handle exception, e.g., print an error message or log the
exception
e.printStackTrace();
return null;
}
}
}
Output:-
2. To write a Python program using SHA Algorithm on generating a Hash Value for a
credit card number "1634 5678 2345 9812"
Source Code :-
import hashlib
def generate_sha256_hash(credit_card_number):
# Create a new SHA-256 hash object
sha256 = hashlib.sha256()
# Update the hash object with the bytes of the credit card number
sha256.update(credit_card_number.encode('utf-8'))
# Get the hexadecimal representation of the hash
hash_value = sha256.hexdigest()
return hash_value
# Credit card number
credit_card_number = "1634 5678 2345 9812"
# Generate SHA-256 hash
hash_value = generate_sha256_hash(credit_card_number)
# Display the generated hash value
print("SHA-256 Hash Value:", hash_value)
Output : -
3. To write a Python program using SHA Algorithm on generating a Hash Value for a
string " GGSIPU".
Source Code :-
import hashlib
def generate_sha256_hash(input_string):
# Create a new SHA-256 hash object
sha256 = hashlib.sha256()
# Update the hash object with the bytes of the input string
sha256.update(input_string.encode('utf-8'))
# Get the hexadecimal representation of the hash
hash_value = sha256.hexdigest()
return hash_value
# Input string
input_string = "GGSIPU"
# Generate SHA-256 hash
hash_value = generate_sha256_hash(input_string)
# Display the generated hash value
print("SHA-256 Hash Value:", hash_value)
Output:-
4. Using REMIX IDE to write a smart contract to generate a hash value using pair of getter
and setter functions
Source code:-
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HashGenerator {
// State variable to store the hash value
bytes32 private hashValue;
// Event to log hash value changes
event HashValueChanged(bytes32 newHashValue);
// Setter function to update the hash value
function setHashValue(string memory input) public {
hashValue = keccak256(abi.encodePacked(input));
emit HashValueChanged(hashValue);
}
// Getter function to retrieve the current hash value
function getHashValue() public view returns (bytes32) {
return hashValue;
}
}
Output:-
5. Using REMIX IDE to write a smart contract to display your own name.
Source Code:-
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract NameDisplay {
// State variable to store the name
string private myName;
// Event to log when the name is set
event NameSet(string newName);
// Function to set your name
function setName(string memory newName) public {
myName = newName;
emit NameSet(newName);
}
// Function to get your name
function getName() public view returns (string memory) {
return myName;
}
}
Output:-
6. Using REMIX IDE to write a smart contract to perform the addition of the two numbers.
Source Code:-
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Adder {
// Function to perform addition of two numbers
function addTwoNumbers(uint256 a, uint256 b) public pure returns (uint256) {
return a + b;
}
}
Output:-
7. Using REMIX IDE write a Smart Contract to generate NFT
Source Code :-
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; // our contract NFT inherits from
openzeppelin's ERC721URIStorage Contract
contract NFT is ERC721URIStorage {
// importing and then declaring a counter
// to keep a track of total tokens minted
using Counters for Counters.Counter;
Counters.Counter private tokenId; // our constructor function calls the ERC721
// passing two arguments for name and symbol respectively
constructor() ERC721("UVLabs", "UVL") {} // defining a function mint which takes a recipient's address and a tokenURI
as an argument
function mint(
address recipientAddress,
string memory tokenURI_
) public returns (uint256 newItemId) {
// increments tokenId value by one
tokenId.increment();
newItemId = tokenId.current(); // _safeMint is a private method
// which mints the next value of the counter
_safeMint(recipientAddress, newItemId);
_setTokenURI(newItemId, tokenURI_);
}
}
Output:-