java-Information Security (i)-base64,md5,sha,hmac

Source: Internet
Author: User
Tags base64 hmac sha1





Overview


Basic concepts of information security:


    • BASE64 encoding Format
    • MD5 (Message Digest algorithm 5, Information Digest algorithm)
    • SHA (Secure Hash algorithm, security hashing algorithm)
    • HMAC (Hash message authentication code, hash messages authentication code)
Base64


As defined by RFC2045, Base64 is defined as: the Base64 content transfer encoding is designed to describe an arbitrary sequence of 8-bit bytes as a form that is not easily recognizable by humans. (The Base64 content-transfer-encoding is designed to represent arbitrary sequences of octets in a form that need not being HU Manly readable.)



Use: Recommended org.apache.commons.codec.binary.Base64


 
   @Test
    public void testEncodeBase64() throws Exception {
        byte[] encodeBase64 = org.apache.commons.codec.binary.Base64
                .encodeBase64("进行Base64".getBytes("UTF-8"));
        System.out.println(new String(encodeBase64));//6L+b6KGMQmFzZTY0
    }

    @Test
    public void testSDecodeBase64() throws Exception {
        byte[] decodeBase64 = org.apache.commons.codec.binary.Base64
                .decodeBase64("6L+b6KGMQmFzZTY0");
        System.out.println(new String(decodeBase64));//进行Base64
    }
MD5


Message Digest algorithm MD5 (Chinese named message Digest Algorithm version fifth) is a hash function widely used in the field of computer security to provide integrity protection for messages.



MD5 is message-digest algorithm 5 (Information-Digest algorithm 5), which is used to ensure complete and consistent information transmission. is one of the widely used hashing algorithms (also translation digest algorithm, hashing algorithm), mainstream programming language has been widely MD5 implemented. The calculation of data (such as Chinese characters) as another fixed length value is the basic principle of the hashing algorithm, and the predecessor of MD5 is MD2, MD3 and MD4.



Use:


 
 @Test
    public void testMD5() throws Exception {
        String md5Msg = msgSafeBase("测试MD5","MD5");
        System.out.println(md5Msg);// c2dbb895a66c3ca924ccdbea49fa6884
    }
    
    
    public String msgSafeBase(String msg, String algorithmName) throws Exception {
        MessageDigest m = MessageDigest.getInstance(algorithmName);
        m.update(msg.getBytes("UTF8"));
        byte s[] = m.digest();
        return Hex.encodeHexString(s);
    }
SHA


Secure Hash algorithm is primarily intended for digital Signature algorithm DSA, which is defined in the digital Signature standard DSS. For messages that are less than 2^64 bits in length, SHA1 produces a 160-bit message digest. The algorithm has been developed and improved by cryptographic experts for many years and is widely used. 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). The hash function value can be said to be a "fingerprint" or "digest" of the plaintext, so the digital signature of the hash value can be regarded as the digital signature of this plaintext.


The Secure Hash algorithm SHA (Secure hash Algorithm,sha) is a national standard FIPS pub 180 released by the National Institute of Standards and Technology, and the latest standards have been updated in 2008 to FIPS pub 180-3. It stipulates the sha-1,sha-224,sha-256,sha-384, and the SHA-512 one-way hashing algorithm. 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.


SHA1 deprecated



Use:


 
  @Test
    public void testSHA() throws Exception {
        // SHA-1,SHA-256,SHA-384,和SHA-512
        String hashMsg = msgSafeBase("test SHA", "SHA-1");
        System.out.println(hashMsg);
        // sha1:9bfec0ff7027c76c28fdaa51bd5a619c5e2f69bb
    }

    public String msgSafeBase(String msg, String algorithmName) throws Exception {
        MessageDigest m = MessageDigest.getInstance(algorithmName);
        m.update(msg.getBytes("UTF8"));
        byte s[] = m.digest();
        return Hex.encodeHexString(s);
    }
Hmac


HMAC is a key-related hash operation message authentication code, and the HMAC operation uses a hashing algorithm to generate a message digest as output with a key and a message as input.



Operational function


(1) Verifying the authorization data and authentication data accepted by the TPM, and (2) confirming that the command request received by the TPM is an authorized request, and that the command has not been altered during transmission. Defining an HMAC requires a cryptographic hash function (denoted as h, which can be MD5 or SHA-1) and a key K. We use B to represent the number of bytes in the data block. (The partition data block of the hash function mentioned above is b=64), and L is used to represent the output data bytes of the hash function (l=20 in L=16,sha-1 in MD5). The length of the authentication key can be any positive integer value that is less than or equal to the word size of the data block. If the key length used in the application is larger than B, it is first used with the hash function h and then the L-length string with the H output as the actual key used in the HMAC. In general, the recommended minimum key k length is l bytes.


Use:


 
   @Test
    public void testHashMsgCode() throws Exception {
        String macKey = initMacKey();
        System.out.println(macKey);
        //vTVhh1xBdDTm9/TZhVsOK0+G/Aw2fkCx0gC6KcM7o2lbCy6DyatcUSe66PTu70E7J0r/hhtodcZBPuLI4/aCgw==
        
        String msgCode=hashMsgCode("test HMAC".getBytes(),macKey);
        System.out.println(msgCode);
        //7e4f0f95cfef2c8f5af9799d03798e76
    }

    public static String initMacKey() throws Exception {
        // HmacMD5,HmacSHA1,HmacSHA256,HmacSHA384,HmacSHA512
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
        SecretKey secretKey = keyGenerator.generateKey();
        return new String(Base64.encodeBase64(secretKey.getEncoded()));
    }

    public static String hashMsgCode(byte[] data, String key) throws Exception {
        SecretKey secretKey = new SecretKeySpec(Base64.decodeBase64(key),
                "HmacMD5");
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);
        return new String(Hex.encodeHex(mac.doFinal(data)));
    }





java-Information Security (i)-base64,md5,sha,hmac


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.