Java Secure Hash functions

Source: Internet
Author: User
Tags packt

Java Secure Hash functions
A Secure Hash function would generate a large number, called the hash value, when given a document of some sort. This document can is of almost any type. We'll be using the simple strings in our examples.
The function is a One-way hash function, which means that it was effectively impossible to recreate the document when given a hash value. When used In conjunction with asymmetric keys, it allows the transmission of a document with the Guarantee that the document had not been altered.
The sender of a document would use a secure hash function to generate the hash value for a document. The sender would Encrypt this hash value with their private key. The document and the key is then combined and sent to a receiver. The document is not encrypted.
Upon receiving the document, the receiver would Use the sender's public key to decrypt the hash value. The receiver would then Use the same secure hash function against the document to obtain a hash value. If This hash value matches the decrypted hash value and then the receiver was guaranteed that the document had not been Modifi Ed.
The intent is Not to encrypt the document. While possible, this approach was useful when it was not important to hide the document from third parties De a guarantee that the document had not been modified.
Java supports the following hashing algorithms:
md5:the default size is bytes
Sha1:the default size is bytes


We will Use the SHA hash function for our Examples. This series is functions was developed by the national Security Agency ( NSA ). There is three versions of this hash function:sha-0, SHA-1, and SHA-2. The SHA-2 is the more secure algorithm and Uses variable Digest sizes:sha-224, SHA-256, SHA-384, and SHA-512.
the MessageDigest class works with arbitrary-sized data producing a fixed size hash value . There is no public constructors for this class. The GetInstance method returns an instance of the class when given the name of the algorithm. Valid names is found At http://docs.oracle.com/javase/8/docs/technotes/ Guides/security/standardnames.html#messagedigest

In this example, we use SHA-256:
MessageDigest messagedigest = messagedigest.getinstance ("SHA-256");
Messagedigest.update (Message.getbytes ());
Package Com.doctor.ch08;import Java.nio.charset.standardcharsets;import Java.security.messagedigest;import Java.security.nosuchalgorithmexception;import javax.xml.bind.datatypeconverter;/** * Secure Hash functions * * @author Sdcuike * * Created on April 16, 2016 PM 8:22:01 */public class Securehashfunctions {public static void main (String        [] args) throws nosuchalgorithmexception {String message = "This was a simple text message";        String HashValue = gethashvalue (message);        System.out.println (HashValue); 83C660972991049C25E6CAD7A5600FC4D7C062C097B9A75C1C4F13238375C26C} static final String hash_algorithm = "SHA-256"    ; static string Gethashvalue (String message) throws NoSuchAlgorithmException {MessageDigest digest = MESSAGEDIGEST.G        Etinstance (Hash_algorithm);        Byte[] B = digest.digest (Message.getbytes (standardcharsets.utf_8));    return datatypeconverter.printhexbinary (b); }}


Results:
83c660972991049c25e6cad7a5600fc4d7c062c097b9a75c1c4f13238375c26c


Reading notes: Learning Network programming with Java
Copyright? Packt Publishing
First Published:december 2015
Production reference:1141215
Published by Packt Publishing Ltd.
Livery Place
Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-547-1
www.packtpub.com


Java Secure Hash functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.