码迷,mamicode.com
首页 > 编程语言 > 详细

java之Secure hash functions

时间:2016-04-17 23:07:50      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:

java之Secure hash functions


A secure hash function will generate a large number, called the hash value, when given a document of some sort. This document can be of almost any type. We will be using simple strings in our examples.
The function is a one-way hash function, which means that it is 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 has not been altered.
The sender of a document will use a secure hash function to generate the hash value for a document. The sender will encrypt this hash value with their private key. The document and the key are then combined and sent to a receiver. The document is not encrypted.
Upon receiving the document, the receiver will use the sender’s public key to decrypt the hash value. The receiver will then use the same secure hash function against the document to obtain a hash value. If this hash value matches the decrypted hash value, then the receiver is guaranteed that the document has not been modified.
The intent is not to encrypt the document. While possible, this approach is useful when it is not important to hide the document from third parties but to only provide a guarantee that the document has not been modified.
Java supports the following hashing algorithms:
MD5: The default size is 64 bytes
SHA1: The default size is 64 bytes


We will use the SHA hash function for our examples. This series of functions was developed by the National Security Agency (NSA). There are 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 are no public constructors for this class. The getInstance method returns an instance of the class when given the name of the algorithm. Valid names are 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 2016年4月16日 下午8:22:01
 */
public class SecureHashFunctions {

    public static void main(String[] args) throws NoSuchAlgorithmException {
        String message = "This is 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.getInstance(hash_algorithm);
        byte[] b = digest.digest(message.getBytes(StandardCharsets.UTF_8));
        return DatatypeConverter.printHexBinary(b);
    }
}


结果:
83C660972991049C25E6CAD7A5600FC4D7C062C097B9A75C1C4F13238375C26C


读书笔记:

Learning Network Programming with Java
Copyright ? 2015 Packt Publishing

First published: December 2015
Production reference: 1141215
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-547-1
www.packtpub.com



java之Secure hash functions

标签:

原文地址:http://blog.csdn.net/doctor_who2004/article/details/51169988

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!