Java basic encryption of SHA encryption

Source: Internet
Author: User

Secure Hash algorithm SHA (Secure hash Algorithm,sha)


The idea of the algorithm is to receive a piece of plaintext, and then convert it into a paragraph (usually smaller) ciphertext in an irreversible way, or simply to take a string of input codes (called Pre-mapping or information) and convert them to shorter lengths, A fixed number of bits of output sequence is the process of hashing values (also known as information digests or information authentication codes).


sha-1,sha-224,sha-256,sha-384, and SHA-512 are some of the one-way hashing algorithms.


sha-1,sha-224 and SHA-256 are suitable for messages that do not exceed 2^64 bits in length.


SHA-384 and SHA-512 are suitable for messages that do not exceed 2^128 bits in length.


package com.soap.util;import java.security.messagedigest;/** *  Secure Hash Algorithm sha (Secure Hash  algorithm,sha)  * *  @author  roger */public class shautil {/**  * sha encryption   Generate 40-bit SHA code  *  *  @param   string to encrypt  *  *  @return   Return 40-bit SHA code  */public static string shaencode (STRING&NBSP;INSTR)  throws  Exception {messagedigest sha = null;try {sha = messagedigest.getinstance (" SHA ");}  catch  (exception e)  {system.out.println (e.tostring ()) e.printstacktrace ();return  "";} Byte[] bytearray = instr.getbytes ("UTF-8"); Byte[] md5bytes = sha.digest ( ByteArray); Stringbuffer hexvalue = new stringbuffer ();for  (int i = 0; i  < md5bytes.length; i++)  {int val =  ((int)  md5bytes[i])  &  0xff;if&nbSP; (val < 16)  {hexvalue.append ("0");} Hexvalue.append (Integer.tohexstring (Val));} Return hexvalue.tostring ();} /** *  Test main functions  *  *  @param  args *  @throws  exception */ Public static void main (string args[])  throws exception {string str  = new string ("123456"); System.out.println ("SHA"  + shaencode (str));}}


Java basic encryption of SHA encryption

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.