java之Secure hash functions
来源:互联网 发布:吉隆坡双子塔网络售票 编辑:程序博客网 时间:2024/05/16 01:57
java之Secure hash functions
A secure hash function will generatea 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 usedin conjunction with asymmetric keys, it allows the transmission of a document with theguarantee that the document has not been altered.
The sender of a document will use a secure hash function togenerate 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 willuse the sender’s public key to decrypt the hash value. The receiver will thenuse 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
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 usedin conjunction with asymmetric keys, it allows the transmission of a document with theguarantee that the document has not been altered.
The sender of a document will use a secure hash function togenerate 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 willuse the sender’s public key to decrypt the hash value. The receiver will thenuse 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 theNational 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 dataproducing 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
The MessageDigest class works with arbitrary-sized dataproducing 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());
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
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
1 0
- java之Secure hash functions
- Hash Functions
- Hash Functions
- Hash Functions
- java之Secure communication terminology
- String hash functions
- RFC4634 US Secure Hash Algorithms
- Useful functions to provide secure PHP application
- Hash Functions for Hash Table Lookup
- Hash Functions for Hash Table Lookup
- Hash Functions for Hash Table Lookup
- Hash Table: Hash Functions--Data Structure
- Learning Perl: 6.3. Hash Functions
- java数据结构之hash表
- SettingsProvider之Secure
- Hash functions: unique result for unique input
- JSch -- Java Secure Channel
- Javascript hash functions to convert string into integer hash
- 文件工具类 FileUtil.java 主要获取文件的Cache目录
- 二叉树中和为某一值的路径 java实现
- Node.js 文件模块(fs)模块介绍
- BestCoder #80 - 1002 快速乘法取模
- mac iterm2有什么酷功能?
- java之Secure hash functions
- 浅谈android官方文档中自定义view的demo
- ACM--数学几何--2140 Forever 0.5--水
- 数组中取出下标不连续的任意个数,求取出的数的和的最大值
- Integer源码浅析
- Some Basic Knowledge
- C-滑雪|DFS+dp
- 基于ENVI的图像融合
- Java Socket探究