In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use Java to generate QR codes with secure hashes. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
First, I needed a library that could handle QR codes, and I decided to use the Zebra Crossing ("ZXing") library because it was easy to use (that is, there was a community around it). Add the following dependency pom.xml:
Com.google.zxingcore3.4.0com.google.zxingjavase3.4.0
The library provides a wide range of functions for generating and reading code. This is enough for my use case, I just need to generate a QR code with a simple JSON object:
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);}
Also notice the interesting little thing JSONObject: it uses Java to convert hash mappings into JSON objects. Sometimes it's much easier to build data structures the way you want, and then serialize them into JSON:
Map qrCodeDataMap = Map.of ("Name", "SampleText", "Key", "SomeHashedValue")
String jsonString = new JSONObject (qrCodeDataMap). ToString ()
To be able to use the JSONObject class, you need to add the following dependencies to your pom.xml:
Org.jsonjson20180813
If you are looking for a more simplified interface, you may also look at QRGen, which claims to further simplify the QR code for Java to generate API and build on ZXing. But in my case, ZXing is absolutely fine.
Hash string
Now, I need to be able to hash encrypted strings in a fast and secure manner. To do this, I decided to use the method recommended by OWASP for Java. To implement this method, you need to update the pom.xml first:
Commons-codeccommons-codec1.12
Here is a (somewhat simplified) implementation of the method described in Java:
Public String generateVerificationKey (String str) throws NoSuchAlgorithmException,InvalidKeySpecException {int iterations = 10000 political int keyLength = 512 salt.getBytes [] 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);}
Thank you for reading! This is the end of the article on "how to use Java to generate QR codes with secure hashes". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.