溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么使用Java生成具有安全哈希的QR碼

發(fā)布時(shí)間:2021-09-26 16:29:29 來源:億速云 閱讀:93 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)怎么使用Java生成具有安全哈希的QR碼的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

首先,需要一個(gè)可以處理QR碼的庫,我決定使用Zebra Crossing(“ZXing”)庫,因?yàn)樗唵我子茫从袊@它的社區(qū))。添加以下依賴項(xiàng)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)足夠了,我只需要生成一個(gè)帶有簡單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 &acute;generateVerificationKey&acute; 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對象。有時(shí),以您希望的方式構(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類,您需要將以下依賴項(xiàng)添加到您的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建議的方法。要實(shí)現(xiàn)此方法,您需要首先更新pom.xml:

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

這里是Java中所述方法的(有些簡化)實(shí)現(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);}

感謝各位的閱讀!關(guān)于“怎么使用Java生成具有安全哈希的QR碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI