這篇文章給大家分享的是有關怎么使用Java生成具有安全哈希的QR碼的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
首先,需要一個可以處理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>
該庫為生成和讀取代碼提供了相當廣泛的功能。這對我的用例來說已經(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,它聲稱可以進一步簡化用于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);}
感謝各位的閱讀!關于“怎么使用Java生成具有安全哈希的QR碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
網(wǎng)頁標題:怎么使用Java生成具有安全哈希的QR碼-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://www.rwnh.cn/article44/djhshe.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站內(nèi)鏈、小程序開發(fā)、建站公司、網(wǎng)站制作、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容