這篇文章主要介紹怎么在Java中使用salt生成QR代碼和安全散列字符串,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供麻栗坡網(wǎng)站建設(shè)、麻栗坡做網(wǎng)站、麻栗坡網(wǎng)站設(shè)計、麻栗坡網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、麻栗坡企業(yè)網(wǎng)站模板建站服務(wù),十載麻栗坡做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
這是關(guān)于如何在Java中使用salt生成QR代碼和安全散列字符串的分步教程。
首先,需要一個可以處理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)足夠了,我只需要生成一個帶有簡單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類,您需要將以下依賴項(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); }
以上是“怎么在Java中使用salt生成QR代碼和安全散列字符串”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享標(biāo)題:怎么在Java中使用salt生成QR代碼和安全散列字符串
文章出自:http://chinadenli.net/article34/joiese.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、App開發(fā)、定制開發(fā)、服務(wù)器托管、App設(shè)計、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)