Digital Signatures - an algorithm to produce the hash and previousHash.


We have our Block created. Next we need to have a system for creating a digital signature. The digital signature is what binds the Blocks together to form a blockchain. A block contains the signature of the previous block and it creates it's digital signature using the previous block's signature. This is very important. Using the previous block's digital signature to create the current block's signature is what actually creates the chain. It's what links them together. It also is the system that will be used to validate that a block is legit. This is the security aspect of blockchain. The fact that every block builds its digital signature from the previous block means that you will have to replicate the entire chain in order to change any data within the chain. As you can guess, this leads to a very difficult task. As the chain grows, so does the security. Changing any data within the chain of blocks will cause the chain to break based on this system. Nobody wants a broken chain. Especially, when it's a public ledger for currency.


Cryptographic Hash ( known as a digest ) is a kind of signature for a text or data file.


SHA-256 is the successor hash function to SHA-1 and is one of the strongest hash functions. It has the capabilities to generate an almost unique 256-bit (32-byte) digital signature.

import; is a java provided class to create SHA-1 and SHA-256 one way hash functions.

   public static String applySha256(String input)
            MessageDigest digest = MessageDigest.getInstance("SHA-256");            
            //Applies sha256 to our input, 
            byte[] hash = digest.digest(input.getBytes("UTF-8"));
            // This will contain hash as hexidecimal
            StringBuffer hexString = new StringBuffer(); 

            for (int i = 0; i < hash.length; i++) {
                String hex = Integer.toHexString(0xff & hash[i]);
                if(hex.length() == 1) hexString.append('0');
            return hexString.toString();
        catch(Exception e)
            throw new RuntimeException(e);

 The code provided is a common cryptographic algorithm using SHA-256. There are many algorithm's to choose from when you create your digital signature. If you search the internet, you can find other's and experiment with the different algorithm's and learn the differences of each algorithm.


Details of the static method:


It requires a variable of String as input and returns a variable of String as output. It applies the SHA-256 one way encryption to the input string. It is important to note that when using the SHA-256 system, it is not considered encryption because you can not decrypt the output String variable. That is why it is referred to as one way.


You can study the static method and explore each line to learn more on how it operates along with trying other algorithm's. For now, just understand it creates the digital signature. Again, I don't enjoy reinventing the wheel. If someone has made a wheel in programming, just use it as long as it's legal.


Real life example. This takes the static method that we discussed and uses it in a real program. Enjoy.

import javax.swing.JFrame;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


 * ExampleSha256 is a class file that provides an example of
 * applying SHA-256 one way encryption to create a digital signature.
 * @author Jason Petty
 * @version 1.0
public class ExampleSha256 implements ActionListener
    // instance variables . I wonder why they are private? Oh well.
    private JTextField varOneTextField;
    private JTextField varTwoTextField;
    private JTextField results1TextField;
    private JTextField results2TextField;
    private JTextField resultsTextField;

     * Constructor for objects of class ExampleSha256
    public ExampleSha256()
        // initialise instance variables
        // Note we did not initialize anything of value here. Why? 
    public static void main ( String[] args )
        // Creates an examle object of this class 
        ExampleSha256 example = new ExampleSha256();
        // Build the gui frame with size and what to do when closed.
        JFrame frame = new JFrame();
        frame.setSize(600, 250);
        // These are the two fields to hold the two variable data values
        example.varOneTextField = new JTextField(20);
        example.varTwoTextField = new JTextField(20);
        // This is all gui code to make the screen look the way it does
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        JLabel titleLabel = new JLabel("Example SHA-256 Generate Digital Signature");

        JPanel onePanel = new JPanel();
        JLabel varOneText = new JLabel("Variable 1 (Type anything you want): ");
        JPanel twoPanel = new JPanel();
        JLabel varTwoText = new JLabel("Variable 2 (Type anything you want): ");
        // The button code to operate when clicked
        JButton theButton = new JButton("Click Me");
        JPanel buttonPanel = new JPanel();
        // More gui code to make it all look good
        JPanel combinedPanel = new JPanel();
        combinedPanel.setLayout(new BoxLayout(combinedPanel, BoxLayout.Y_AXIS));
        // These are the result fields used to display the signatures for the user
        example.resultsTextField = new JTextField(50);
        example.results1TextField = new JTextField(50);
        example.results2TextField = new JTextField(50);
        // more gui code to make it look awesome
        JPanel r1Panel = new JPanel();
        JPanel r2Panel = new JPanel();
        JPanel rPanel = new JPanel();
        JPanel totalRPanel = new JPanel();
        totalRPanel.setLayout(new BoxLayout(totalRPanel, BoxLayout.Y_AXIS));
        // This puts all this cool gui code into the window frame

        // Makes the frame visible
        // Just a blank line for fun.


    public static String applySha256(String input)
        // This is an algorithm for applying the SHA-256 to a String variable. Who knows what this does? Not me?
        // Maybe i do know. I just dont want to tell you. Hum....
            MessageDigest digest = MessageDigest.getInstance("SHA-256");            
            //Applies sha256 to our input, 
            byte[] hash = digest.digest(input.getBytes("UTF-8"));           
            StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal
            for (int i = 0; i < hash.length; i++) {
                String hex = Integer.toHexString(0xff & hash[i]);
                if(hex.length() == 1) hexString.append('0');
            return hexString.toString();
        catch(Exception e)
            // I know this line means to throw an error if something goes wrong.
            throw new RuntimeException(e);
    public void actionPerformed(ActionEvent event)
        // This is what happens when u click my button.
        // It grabs the 2 variables and proceeds to produce digital signatures
        // and display it so you can actually look at it.
        String results1 = varOneTextField.getText();
        String results2 = varTwoTextField.getText();
        String results = applySha256(results1);
        String displayText = "Var 1 Signature: " + results;
        results = applySha256(results2);
        displayText = "Var 2 Signature: " + results;
        results = applySha256(results1+results2);
        displayText = "  Combined Sig: " + results;
        // whew commenting code is no fun.


 Next Tutorial? Anyone enjoy this one? It only gets better as we go!