转载

使用Java生成具有安全哈希的QR码 | Rustam Mehmandarov

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

首先,需要一个可以处理QR码的库,我决定使用 Zebra Crossing(“ZXing”) 库,因为它简单易用(即有围绕它的社区)。添加以下依赖项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>

该库为生成和读取代码提供了相当广泛的功能。这对我的用例来说已经足够了,我只需要生成一个带有简单JSON对象的QR代码:

<b>public</b> byte[] qrCodeGenerator(String id) throws IOException, 
                                                WriterException, 
                                                InvalidKeySpecException, 
                                                NoSuchAlgorithmException {

    String filePath = <font>"QRCode.png"</font><font>;
    String charset = </font><font>"UTF-8"</font><font>;
    Map hintMap = <b>new</b> HashMap();
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

    Map<String, String> qrCodeDataMap = Map.of(
            </font><font>"Name"</font><font>, id,
            </font><font>"Key"</font><font>, keyProvider.generateVerificationKey(id) 
            </font><font><i>// see next section for ´generateVerificationKey´ method</i></font><font>
    );

    String jsonString = <b>new</b> JSONObject(qrCodeDataMap).toString();
    createQRCode(jsonString, filePath, charset, hintMap, 500, 500);

    BufferedImage image = ImageIO.read(<b>new</b> File(filePath));
    ByteArrayOutputStream baos = <b>new</b> ByteArrayOutputStream();
    ImageIO.write(image, </font><font>"png"</font><font>, baos);
    byte[] imageData = baos.toByteArray();

    <b>return</b> imageData;
}

<b>private</b> <b>void</b> createQRCode(String qrCodeData, 
                          String filePath, 
                          String charset, 
                          Map hintMap, 
                          <b>int</b> qrCodeHeight, 
                          <b>int</b> qrCodeWidth) throws WriterException, 
                                                  IOException {

    BitMatrix matrix = <b>new</b> MultiFormatWriter().encode(
            <b>new</b> String(qrCodeData.getBytes(charset), charset),
            BarcodeFormat.QR_CODE,
            qrCodeWidth,
            qrCodeHeight,
            hintMap
    );

    MatrixToImageWriter.writeToPath(
            matrix,
            filePath.substring(filePath.lastIndexOf('.') + 1),
            FileSystems.getDefault().getPath(filePath)
    );
}
</font>

还要注意有趣的小东西 JSONObject:是使用Java将哈希映射转换为JSON对象。有时,以您希望的方式构建数据结构要容易得多,然后序列化为JSON:

Map<String, String> qrCodeDataMap = Map.of(
        <font>"Name"</font><font>, </font><font>"SampleText"</font><font>,
        </font><font>"Key"</font><font>, </font><font>"SomeHashedValue"</font><font>
);
String jsonString = <b>new</b> JSONObject(qrCodeDataMap).toString();
</font>

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

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

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

哈希字符串

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

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

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

<b>public</b> String generateVerificationKey(String str) throws NoSuchAlgorithmException,
                                                         InvalidKeySpecException {
    <b>int</b> iterations = 10000;
    <b>int</b> keyLength = 512;

    <b>char</b>[] strChars = str.toCharArray();
    byte[] saltBytes = salt.getBytes();

    SecretKeyFactory skf = SecretKeyFactory.getInstance(<font>"PBKDF2WithHmacSHA512"</font><font>);
    PBEKeySpec spec = <b>new</b> PBEKeySpec(strChars, saltBytes, iterations, keyLength);
    SecretKey key = skf.generateSecret( spec );
    byte[] hashedBytes = key.getEncoded( );

    <b>return</b> Hex.encodeHexString(hashedBytes);
}
</font>
原文  https://www.jdon.com/52501
正文到此结束
Loading...