本文整理匯總了Java中java.security.Key.getFormat方法的典型用法代碼示例。如果您正苦於以下問題:Java Key.getFormat方法的具體用法?Java Key.getFormat怎麽用?Java Key.getFormat使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.security.Key
的用法示例。
在下文中一共展示了Key.getFormat方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: formatKey
import java.security.Key; //導入方法依賴的package包/類
public static String formatKey(Key key) {
String algo = key.getAlgorithm();
String fmt = key.getFormat();
byte[] encoded = key.getEncoded();
return "Key[algorithm=" + algo + ", format=" + fmt +
", bytes=" + encoded.length + "]";
}
示例2: applyMAC
import java.security.Key; //導入方法依賴的package包/類
private void applyMAC(Key key) throws Exception {
SecretKey key2 = new SecretKey() {
@Override
public String getFormat() {
// TODO Auto-generated method stub
return key.getFormat();
}
@Override
public byte[] getEncoded() {
// TODO Auto-generated method stub
String hitesh = "This is from Hitesh";
byte[] secbytes = hitesh.getBytes();
byte[] origsecret = key.getEncoded();
byte[] ns = new byte[origsecret.length + secbytes.length];
System.arraycopy(origsecret, 0, ns, 0, origsecret.length);
System.arraycopy(secbytes, 0, ns, origsecret.length, secbytes.length);
return ns;
}
@Override
public String getAlgorithm() {
// TODO Auto-generated method stub
return key.getAlgorithm();
}
};
// Generate secret key for HMAC-MD5
// KeyGenerator kg = KeyGenerator.getInstance("HmacMD5");
// SecretKey sk = kg.generateKey();
// Get instance of Mac object implementing HMAC-MD5, and
// initialize it with the above secret key
System.out.println("Key2 secret " + toHexString(key2.getEncoded()));
Mac mac = Mac.getInstance("HmacMD5");
mac.init(key2);
byte[] result = mac.doFinal("Hi There".getBytes());
System.out.println("Message Authentication code length: " + mac.getMacLength());
System.out.println("Message Authentication code : " + toHexString(result));
verifyMacBody(mac, result);
}