Compute HMAC-SHA512 with secret key in java

后端 未结 3 676
说谎
说谎 2020-12-02 23:32

i want to build excatly a function which produces a HMAC with a secret key like this site provides:

http://www.freeformatter.com/hmac-generator.html

The java

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 23:55

    Also you can use a little wrapper by Apache Commons codec:

    import static org.apache.commons.codec.digest.HmacAlgorithms.HMAC_SHA_512;
    
    import org.apache.commons.codec.digest.HmacUtils;
    
    public class HmacService {
    
        private String sharedSecret;
    
        public HmacService(String sharedSecret) {
            this.sharedSecret = sharedSecret;
        }
    
        public String calculateHmac(String data) {
            return new HmacUtils(HMAC_SHA_512, sharedSecret).hmacHex(data);
        }
    
        public boolean checkHmac(String data, String hmacHex) {
            return calculateHmac(data).equals(hmacHex);
        }
    
    }
    

提交回复
热议问题