java之Secure hash functions

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

时间: 2024-10-26 17:54:57

java之Secure hash functions的相关文章

java之Secure communication terminology

java之Secure communication terminology There are several terms that are used when working with secure communications. These include the following: Authentication: This is the process of verifying a user or system Authorization: This is the process of

Java 散列表 hash table

Java 散列表 hash table @author ixenos hash table, HashTable, HashMap, HashSet hash table 是一种数据结构 hash table 为每个对象计算一个整数,该整数被称为散列码 hash code hash code 是由对象的实例域产生的一个整数,具有不同的数据域的对象将产生不同的hash code 如果自定义类,就要负责实现这个类的hashCode方法,注意要与equals方法兼容,即如果a.equals(b)为tr

SHA1 安全哈希算法(Secure Hash Algorithm)

安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准 (Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signature Algorithm DSA).对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要.当接收到消息的时候,这个消息摘要可以用来验证数据的完整性.在传输的过程中,数据很可能会发生变化,那么这时候就会产生不同的消息摘要. SHA1有如下特性:不可以从消息摘要中复原信息:两个不同的消息不

What Are Hash Functions

What Are Hash Functions A hash function is simply a function that takes in input value, and from that input creates an output value deterministic of the input value. For any x input value, you will always receive the same y output value whenever the

Java 获取字符串Hash值

Java 生成字符串的Hash值: /** * A hashing method that changes a string (like a URL) into a hash suitable for using as a * disk filename. */ public static String hashKeyForDisk(String key) { String cacheKey; try { final MessageDigest mDigest = MessageDigest.g

Java 学习之 Hash

Hash Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,所以不可能从散列值来唯一的确定输入值.简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数. HASH函数(计算机算法领域) 基本概念 * 若结构中存在和关键字K相等的记录,则必定在f(K)的存储位置上.由

IA_11.3散列函数Hash functions

spark机器学习-第3章

1.安装工具ipython https://www.continuum.io/downloads 选择自己需要的版本 2.安装过程 (1)赋权限 chmod u+x ./Anaconda2-4.2.0-Linux-x86_64.sh (2)回车 [[email protected] tool]# ./Anaconda2-4.2.0-Linux-x86_64.sh Welcome to Anaconda2 4.2.0 (by Continuum Analytics, Inc.) In order

Windows Server 2008 R2 下配置TLS1.2,添加自签名证书

前言 2017年1月1日起App Store上的所有App应用将强制开启ATS功能. 苹果的ATS(App Transport Security)对服务器硬性3点要求: ① ATS要求TLS1.2或者更高,TLS 是 SSL 新的别称. ② 通讯中的加密套件配置要求支持列出的正向保密. ③ 数字证书必须使用sha256或者更高级的签名哈希算法,并且保证密钥是2048位及以上的RSA密钥或者256位及以上的ECC密钥. 由于领导舍不得花钱,只能辛苦我们自己搞个不花钱的证书.在网上找了一大堆各种配置