亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何使用Java生成具有安全哈希的QR碼

 更新時間:2019年06月19日 11:02:29   作者:Java_蘇先生  
這篇文章主要介紹了如何使用Java生成具有安全哈希的QR碼,這是關(guān)于如何在Java中使用salt生成QR代碼和安全散列字符串的分步教程。,需要的朋友可以參考下

這是關(guān)于如何在Java中使用salt生成QR代碼和安全散列字符串的分步教程。

首先,需要一個可以處理QR碼的庫,我決定使用Zebra Crossing(“ZXing”)庫,因為它簡單易用(即有圍繞它的社區(qū))。添加以下依賴項pom.xml:

<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>

該庫為生成和讀取代碼提供了相當(dāng)廣泛的功能。這對我的用例來說已經(jīng)足夠了,我只需要生成一個帶有簡單JSON對象的QR代碼:

public byte[] qrCodeGenerator(String id) throws IOException, 
WriterException, 
InvalidKeySpecException, 
NoSuchAlgorithmException {
String filePath = "QRCode.png";
String charset = "UTF-8";
Map hintMap = new HashMap();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
Map<String, String> qrCodeDataMap = Map.of(
"Name", id,
"Key", keyProvider.generateVerificationKey(id) 
// see next section for ´generateVerificationKey´ method
);
String jsonString = new JSONObject(qrCodeDataMap).toString();
createQRCode(jsonString, filePath, charset, hintMap, 500, 500);
BufferedImage image = ImageIO.read(new File(filePath));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
byte[] imageData = baos.toByteArray();
return imageData;
}
private void createQRCode(String qrCodeData, 
String filePath, 
String charset, 
Map hintMap, 
int qrCodeHeight, 
int qrCodeWidth) throws WriterException, 
IOException {
BitMatrix matrix = new MultiFormatWriter().encode(
new String(qrCodeData.getBytes(charset), charset),
BarcodeFormat.QR_CODE,
qrCodeWidth,
qrCodeHeight,
hintMap
);
MatrixToImageWriter.writeToPath(
matrix,
filePath.substring(filePath.lastIndexOf('.') + 1),
FileSystems.getDefault().getPath(filePath)
);
}

還要注意有趣的小東西 JSONObject:是使用Java將哈希映射轉(zhuǎn)換為JSON對象。有時,以您希望的方式構(gòu)建數(shù)據(jù)結(jié)構(gòu)要容易得多,然后序列化為JSON:

Map<String, String> qrCodeDataMap = Map.of(
"Name", "SampleText",
"Key", "SomeHashedValue"
);

String jsonString = new JSONObject(qrCodeDataMap).toString();    

為了能夠使用JSONObject類,您需要將以下依賴項添加到您的pom.xml:

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>

如果您正在尋找更簡化的接口,您可能還會查看QRGen,它聲稱可以進(jìn)一步簡化用于Java的QR代碼生成API,并且構(gòu)建在ZXing之上。但是,在我的情況下,ZXing絕對沒問題。

哈希字符串

現(xiàn)在,我需要能夠以快速安全的方式哈希加密字符串。為此,我決定使用OWASP for Java建議的方法。要實現(xiàn)此方法,您需要首先更新pom.xml:

<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.12</version>
</dependency>

這里是Java中所述方法的(有些簡化)實現(xiàn):

public String generateVerificationKey(String str) throws NoSuchAlgorithmException,
InvalidKeySpecException {
int iterations = 10000;
int keyLength = 512;
char[] strChars = str.toCharArray();
byte[] saltBytes = salt.getBytes();
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
PBEKeySpec spec = new PBEKeySpec(strChars, saltBytes, iterations, keyLength);
SecretKey key = skf.generateSecret( spec );
byte[] hashedBytes = key.getEncoded( );
return Hex.encodeHexString(hashedBytes);
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論