Showing posts with label Java basics. Show all posts
Showing posts with label Java basics. Show all posts

Java SHA-256

Secure Hash Algorithm-256:
Most of the cryptographic hash functions runs on the digital data.
Using SHA-256 algorithm we can determine the data integrity.

 For example, computing the hash of a downloaded file and comparing the result to a previously published hash result can show whether the download has been modified or tampered with some other content.

In addition, cryptographic hash functions are extremely collision-resistant.

Cryptographic hash functions:
   It takes input of any length and gives us an output in fixed length.

Collision-resistant ?
In other words, it should be extremely difficult to produce the same hash output from two different input values using a cryptographic hash function.

This is the example to print the SHA-256 value for given string:

import java.io.*;
import java.util.*;
import java.security.*;
import java.math.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        try{
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        System.out.printf("%064x\n", new BigInteger(1, md.digest(s.getBytes())));
        }catch(NoSuchAlgorithmException e){
            //Handle exception
        }
    }
}

Also, It consists of six identical hashing algorithms(i.e., SHA-256, SHA-512, SHA-224, SHA-384, SHA-512/224, SHA-512/256) with a variable digest size.

SHA-256 is a 256-bit ( 32 byte) hashing algorithm which can calculate a hash code for an input of up to 264-1 bits. It undergoes 64 rounds of hashing and calculates a hash code that is a 64-digit hexadecimal number.

Source: https://www.hackerrank.com

Java MD5

 Message Digest algorithm version 5:

MD5 is a widely-used cryptographic hash function with a -bit hash value. Here are some common uses for MD5:
  • To store a one-way hash of a password.
  • To provide some assurance that a transferred file has arrived intact.
Note: It has been compromised using Flame malware in 2012.

This is the example to encrypt the alphanumeric string in java.

We have package named security in java(Check here).

Below code take the string as input and encrypt using MD5 algorithm.
import java.io.*;
import java.util.*;
import java.security.*;
import java.math.BigInteger;

public class MleClass {

    public static void main(String[] args) {
     
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        try{
        MessageDigest md=  MessageDigest.getInstance("MD5");
        
        System.out.printf("%032x\n", new BigInteger(1, md.digest(s.getBytes())));
        }catch(NoSuchAlgorithmException e){
            // Handle exceptions
        }
        
    }
}


Java this keyword.

this: is called as reference variable because it always refer an current object.

Eg:
Is this valid ?
public class MleClass{
int value=10;
 void show(){
 System.out.println(value);
 }

 public static void main(){
  MleClass obj =new MleClass();
  ob.show();
 }
}

Obj is local variable i.e we can't access that variable outside main method.

In above code snippet value is a instance variable.

So we must need  an object or object or object reference to access value;

Then still valid the above snippet ?

Types of java variables

Java variables are very straightforward, just they are variables.

Yes, just they are variable but we have to understand when the memory allocated and how can we access those.

I have seen some people who can write the code without knowing all this.

Seriously!, is that possible ?

Rules for Java source file.

#Rule-1

A source file can have only one public class.
              Why? - If we give public for multiple classes in single java file , It is ambiguous for JVM.
           It is ambiguous ?
      Then what exactly JVM doing with Public class ?

    Important rules for Overriding

    What is overriding ?
    Just simple definition is "Two or more methods with the same name and same parameter list then it said to be Method overriding" .
    Eg-1:

    class A{
      void show(){
       System.out.println("Overridden method");
       }
      }
      public class B extends A{
      void show(){
       System.out.println("Overriding method");
       }
      }