当前位置: 移动技术网 > IT编程>软件设计>架构 > 使用Java生成具有安全哈希的QR码

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

2019年06月17日  | 移动技术网IT编程  | 我要评论

这是关于如何在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代码:

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将哈希映射转换为json对象。有时,以您希望的方式构建数据结构要容易得多,然后序列化为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,并且构建在zxing之上。但是,在我的情况下,zxing绝对没问题。

哈希字符串

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

<dependency>
  <groupid>commons-codec</groupid>
  <artifactid>commons-codec</artifactid>
  <version>1.12</version>
</dependency>

这里是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);
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网