Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the HMAC algorithm?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you what the HMAC algorithm is, which is concise and easy to understand. It can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Use

The HMAC algorithm is mainly used for authentication, and its usage is as follows:

The client sends out a login request

The server returns a random value, which is saved in the session

The client uses the random value as the key, and the user password performs hmac operation, which is submitted to the server.

The server reads the user and password in the database, uses the key to do the same hmac operation as the client, and then compares it with the result sent by the user. If the result is consistent, the user's identity is legal.

The advantage is that even if the hackers intercept the data we send, they can only get the result after hmac encryption. Because they do not know the key, it is impossible to get the user's password, thus ensuring the security.

The type of HMAC, the type of algorithm, the use of summary length HmacMD5 128HmacSHA1 160HmacSHA256 256HmacSHA384 384HmacSHA512 512HMAC.

If you want to use the HMAC algorithm, you need to generate a key.

For example, you can use keyGenerator that comes with JDK.

Public static byte [] getSecretKey () throws Exception {KeyGenerator keyGenerator = KeyGenerator.getInstance ("HmacMD5"); / / SecretKey key = keyGenerator.generateKey (), such as HmacSHA1,HmacSHA256; byte [] keyBytes = key.getEncoded (); return keyBytes;}

Once the key is obtained, the message digest algorithm can be executed.

Public static String encryptHmac (byte [] key, byte [] data) throws Exception {SecretKey secretKey = new SecretKeySpec (key, "HmacMD5"); Mac mac = Mac.getInstance ("HmacMD5"); mac.init (secretKey); byte [] resultBytes = mac.doFinal (data); String resultString = byteToHexString (resultBytes); return resultString;} this is what HMAC algorithm is. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report