2016-09-06 21 views
8

tôi muốn xây dựng excatly một chức năng mà tạo ra một HMAC với một khóa bí mật như trang web này cung cấp:Tính HMAC-SHA512 với khóa bí mật trong java

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

Các java 8 lib chỉ cung cấp MessageDigest và KeyGenerator mà cả hai chỉ hỗ trợ lên đến SH256.

Ngoài ra, google không cung cấp cho tôi bất kỳ kết quả nào để triển khai để tạo HMAC.

Có ai đó biết triển khai không?

tôi có mã này để tạo ra một SH256 bình thường nhưng tôi đoán điều này không giúp tôi nhiều:

public static String get_SHA_512_SecurePassword(String passwordToHash) throws Exception { 
    String generatedPassword = null; 

    MessageDigest md = MessageDigest.getInstance("SHA-512"); 
    byte[] bytes = md.digest(passwordToHash.getBytes("UTF-8")); 
    StringBuilder sb = new StringBuilder(); 
    for (int i = 0; i < bytes.length; i++) { 
     sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); 
    } 
    generatedPassword = sb.toString(); 
    System.out.println(generatedPassword); 
    return generatedPassword; 
} 

Trả lời

10

Hope this helps:

import java.io.UnsupportedEncodingException; 
import java.security.InvalidKeyException; 
import java.security.NoSuchAlgorithmException; 
import javax.crypto.Mac; 
import javax.crypto.spec.SecretKeySpec; 

public class Test1 { 
public static void main(String[] args) { 
    Mac sha512_HMAC = null; 
    String result = null; 
    String key = "Welcome1"; 

    try{ 
     byte [] byteKey = key.getBytes("UTF-8"); 
     final String HMAC_SHA512 = "HmacSHA512"; 
     sha512_HMAC = Mac.getInstance(HMAC_SHA512);  
     SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA512); 
     sha512_HMAC.init(keySpec); 
     byte [] mac_data = sha512_HMAC. 
     doFinal("My message".getBytes("UTF-8")); 
     //result = Base64.encode(mac_data); 
     result = bytesToHex(mac_data); 
     System.out.println(result); 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (NoSuchAlgorithmException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (InvalidKeyException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }finally{ 
     System.out.println("Done"); 
    } 
} 

public static String bytesToHex(byte[] bytes) { 
    final char[] hexArray = "ABCDEF".toCharArray(); 
    char[] hexChars = new char[bytes.length * 2]; 
    for (int j = 0; j < bytes.length; j++) { 
     int v = bytes[j] & 0xFF; 
     hexChars[j * 2] = hexArray[v >>> 4]; 
     hexChars[j * 2 + 1] = hexArray[v & 0x0F]; 
    } 
    return new String(hexChars); 
} 
} 

Để chuyển đổi từ mảng byte để hex tham khảo stackoverflow này trả lời: here

+0

bạn đời cảm ơn bạn rất nhiều :) công việc của mình – PowerFlower

4

cách đơn giản nhất có thể -

private static final String HMAC_SHA512 = "HmacSHA512"; 

private static String toHexString(byte[] bytes) { 
    Formatter formatter = new Formatter(); 
    for (byte b : bytes) { 
     formatter.format("%02x", b); 
    } 
    return formatter.toString(); 
} 

public static String calculateHMAC(String data, String key) 
    throws SignatureException, NoSuchAlgorithmException, InvalidKeyException 
{ 
    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), HMAC_SHA512); 
    Mac mac = Mac.getInstance(HMAC_SHA512); 
    mac.init(secretKeySpec); 
    return toHexString(mac.doFinal(data.getBytes())); 
} 

public static void main(String[] args) throws Exception { 
    String hmac = calculateHMAC("data", "key"); 
    System.out.println(hmac); 
} 

Bạn có thể thay đổi biến HMAC_SHA512 thành bất kỳ thuật toán Mac nào và mã sẽ hoạt động theo cùng một cách.

Các vấn đề liên quan