博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Java生成具有安全哈希的QR码
阅读量:6879 次
发布时间:2019-06-26

本文共 3458 字,大约阅读时间需要 11 分钟。

这是关于如何在Java中使用salt生成QR代码和安全散列字符串的分步教程。

首先,需要一个可以处理QR码的库,我决定使用Zebra Crossing(“ZXing”)库,因为它简单易用(即有围绕它的社区)。添加以下依赖项pom.xml:

com.google.zxing
core
3.4.0
com.google.zxing
javase
3.4.0

该库为生成和读取代码提供了相当广泛的功能。这对我的用例来说已经足够了,我只需要生成一个带有简单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
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将哈希映射转换为JSON对象。有时,以您希望的方式构建数据结构要容易得多,然后序列化为JSON:

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

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

为了能够使用JSONObject类,您需要将以下依赖项添加到您的pom.xml:

org.json
json
20180813

如果您正在寻找更简化的接口,您可能还会查看QRGen,它声称可以进一步简化用于Java的QR代码生成API,并且构建在ZXing之上。但是,在我的情况下,ZXing绝对没问题。

哈希字符串

现在,我需要能够以快速安全的方式哈希加密字符串。为此,我决定使用OWASP for Java建议的方法。要实现此方法,您需要首先更新pom.xml:

commons-codec
commons-codec
1.12

这里是Java中所述方法的(有些简化)实现:

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);}

转载于:https://www.cnblogs.com/Java-no-1/p/11033032.html

你可能感兴趣的文章
获取HTML源码(只取文字,判断编码,过滤标签)
查看>>
我的架构经验系列文章 - 后端架构 - 设计层面
查看>>
vc生成静态库例子
查看>>
那些帮助你成为优秀前端工程师的讲座——《CSS篇》
查看>>
CheckBox为CheckBoxList实现全选或全取消选择
查看>>
Windbg中使用查找内存并设置访问断点
查看>>
数据库分区表的使用
查看>>
【SAS NOTES】_NULL_
查看>>
辨别delegate方法的好办法
查看>>
C#实现的几种委托
查看>>
Hypertable 0.9.7.1 发布,分布式数据库
查看>>
PHP获得真实客户端的真实IP REMOTE_ADDR,HTTP_CLIENT_IP,HTTP_X_FORWARDED_FOR
查看>>
poj1323
查看>>
c getline
查看>>
linux下拷贝的时候有时候会出现cp:omitting directory的错误
查看>>
C#简单的多线程
查看>>
HTML转换成字符串
查看>>
关于使用CTE(公用表表达式)的递归查询
查看>>
C#简单的面试题目(五)
查看>>
讲故事学Socket编程
查看>>