1 package edu.jiangxin.apktoolbox.file.checksum.panel; 2 3 import edu.jiangxin.apktoolbox.file.checksum.CalculateType; 4 import edu.jiangxin.apktoolbox.swing.extend.EasyChildTabbedPanel; 5 import edu.jiangxin.apktoolbox.utils.Constants; 6 import org.apache.commons.codec.digest.DigestUtils; 7 8 import javax.swing.*; 9 import java.io.ByteArrayInputStream; 10 import java.io.IOException; 11 import java.nio.charset.StandardCharsets; 12 import java.util.zip.CRC32; 13 14 public class StringHashPanel extends EasyChildTabbedPanel { 15 private static final long serialVersionUID = 63924900336217723L; 16 17 private JPanel stringInputPanel; 18 private JTextArea stringInputTextArea; 19 20 private JPanel optionPanel; 21 22 private JTextField stringLengthTextField; 23 private JCheckBox md5CheckBox; 24 private JTextField md5TextField; 25 private JCheckBox sha1CheckBox; 26 private JTextField sha1TextField; 27 private JCheckBox sha256CheckBox; 28 private JTextField sha256TextField; 29 private JCheckBox sha384CheckBox; 30 private JTextField sha384TextField; 31 private JCheckBox sha512CheckBox; 32 private JTextField sha512TextField; 33 private JCheckBox crc32CheckBox; 34 private JTextField crc32TextField; 35 36 private JPanel operationPanel; 37 private JProgressBar progressBar; 38 39 @Override 40 public void createUI() { 41 BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS); 42 setLayout(boxLayout); 43 44 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 45 createStringInputPanel(); 46 add(stringInputPanel); 47 48 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 49 createOptionPanel(); 50 add(optionPanel); 51 52 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 53 createOperationPanel(); 54 add(operationPanel); 55 } 56 57 private void createStringInputPanel() { 58 stringInputPanel = new JPanel(); 59 BoxLayout boxLayout = new BoxLayout(stringInputPanel, BoxLayout.Y_AXIS); 60 stringInputPanel.setLayout(boxLayout); 61 62 JLabel stringInputLabel = new JLabel("Insert string to compute hash checksum:"); 63 stringInputTextArea = new JTextArea(5, 4); 64 stringInputTextArea.setLineWrap(true); 65 JScrollPane stringInputScrollPanel = new JScrollPane(stringInputTextArea); 66 67 stringInputPanel.add(stringInputLabel); 68 stringInputPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 69 stringInputPanel.add(stringInputScrollPanel); 70 } 71 72 private void createOptionPanel() { 73 optionPanel = new JPanel(); 74 BoxLayout boxLayout = new BoxLayout(optionPanel, BoxLayout.Y_AXIS); 75 optionPanel.setLayout(boxLayout); 76 77 JPanel stringLengthOptionPanel = new JPanel(); 78 JPanel md5OptionPanel = new JPanel(); 79 JPanel sha1OptionPanel = new JPanel(); 80 JPanel sha256OptionPanel = new JPanel(); 81 JPanel sha384OptionPanel = new JPanel(); 82 JPanel sha512OptionPanel = new JPanel(); 83 JPanel crc32OptionPanel = new JPanel(); 84 85 optionPanel.add(stringLengthOptionPanel); 86 optionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 87 optionPanel.add(md5OptionPanel); 88 optionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 89 optionPanel.add(sha1OptionPanel); 90 optionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 91 optionPanel.add(sha256OptionPanel); 92 optionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 93 optionPanel.add(sha384OptionPanel); 94 optionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 95 optionPanel.add(sha512OptionPanel); 96 optionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER)); 97 optionPanel.add(crc32OptionPanel); 98 99 stringLengthOptionPanel.setLayout(new BoxLayout(stringLengthOptionPanel, BoxLayout.X_AXIS)); 100 JLabel stringLengthLabel = new JLabel("String length:"); 101 stringLengthTextField = new JTextField(); 102 stringLengthOptionPanel.add(stringLengthLabel); 103 stringLengthOptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 104 stringLengthOptionPanel.add(stringLengthTextField); 105 106 md5OptionPanel.setLayout(new BoxLayout(md5OptionPanel, BoxLayout.X_AXIS)); 107 md5CheckBox = new JCheckBox("MD5 checksum:"); 108 md5CheckBox.setSelected(true); 109 md5TextField = new JTextField(); 110 md5OptionPanel.add(md5CheckBox); 111 md5OptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 112 md5OptionPanel.add(md5TextField); 113 114 sha1OptionPanel.setLayout(new BoxLayout(sha1OptionPanel, BoxLayout.X_AXIS)); 115 sha1CheckBox = new JCheckBox("SHA1 checksum:"); 116 sha1TextField = new JTextField(); 117 sha1OptionPanel.add(sha1CheckBox); 118 sha1OptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 119 sha1OptionPanel.add(sha1TextField); 120 121 sha256OptionPanel.setLayout(new BoxLayout(sha256OptionPanel, BoxLayout.X_AXIS)); 122 sha256CheckBox = new JCheckBox("SHA256 checksum:"); 123 sha256TextField = new JTextField(); 124 sha256OptionPanel.add(sha256CheckBox); 125 sha256OptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 126 sha256OptionPanel.add(sha256TextField); 127 128 sha384OptionPanel.setLayout(new BoxLayout(sha384OptionPanel, BoxLayout.X_AXIS)); 129 sha384CheckBox = new JCheckBox("SHA384 checksum:"); 130 sha384TextField = new JTextField(); 131 sha384OptionPanel.add(sha384CheckBox); 132 sha384OptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 133 sha384OptionPanel.add(sha384TextField); 134 135 sha512OptionPanel.setLayout(new BoxLayout(sha512OptionPanel, BoxLayout.X_AXIS)); 136 sha512CheckBox = new JCheckBox("SHA512 checksum:"); 137 sha512TextField = new JTextField(); 138 sha512OptionPanel.add(sha512CheckBox); 139 sha512OptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 140 sha512OptionPanel.add(sha512TextField); 141 142 crc32OptionPanel.setLayout(new BoxLayout(crc32OptionPanel, BoxLayout.X_AXIS)); 143 crc32CheckBox = new JCheckBox("CRC32 checksum:"); 144 crc32TextField = new JTextField(); 145 crc32OptionPanel.add(crc32CheckBox); 146 crc32OptionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 147 crc32OptionPanel.add(crc32TextField); 148 } 149 150 private void createOperationPanel() { 151 operationPanel = new JPanel(); 152 153 JButton compareButton = new JButton("Compare"); 154 compareButton.setFocusPainted(false); 155 compareButton.addActionListener(arg0 -> { 156 String text = stringInputTextArea.getText(); 157 if (text == null) { 158 logger.error("text is null"); 159 return; 160 } 161 calculate(text); 162 }); 163 164 progressBar = new JProgressBar(); 165 progressBar.setStringPainted(true); 166 progressBar.setMaximum(100); 167 168 169 operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.X_AXIS)); 170 operationPanel.add(compareButton); 171 operationPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 172 operationPanel.add(progressBar); 173 174 } 175 176 private void calculate(String text) { 177 progressBar.setValue(0); 178 new Thread(() -> { 179 stringLengthTextField.setText(String.valueOf(text.length())); 180 progressBar.setValue(progressBar.getValue() + 10); 181 }).start(); 182 183 new Thread(() -> { 184 if (md5CheckBox.isSelected()) { 185 md5TextField.setText(calculate(CalculateType.Md5, text)); 186 } else { 187 md5TextField.setText(""); 188 } 189 progressBar.setValue(progressBar.getValue() + 15); 190 }).start(); 191 192 new Thread(() -> { 193 if (sha1CheckBox.isSelected()) { 194 sha1TextField.setText(calculate(CalculateType.Sha1, text)); 195 } else { 196 sha1TextField.setText(""); 197 } 198 progressBar.setValue(progressBar.getValue() + 15); 199 }).start(); 200 201 new Thread(() -> { 202 if (sha256CheckBox.isSelected()) { 203 sha256TextField.setText(calculate(CalculateType.Sha256, text)); 204 } else { 205 sha256TextField.setText(""); 206 } 207 progressBar.setValue(progressBar.getValue() + 15); 208 }).start(); 209 210 new Thread(() -> { 211 if (sha384CheckBox.isSelected()) { 212 sha384TextField.setText(calculate(CalculateType.Sha384, text)); 213 } else { 214 sha384TextField.setText(""); 215 } 216 progressBar.setValue(progressBar.getValue() + 15); 217 }).start(); 218 219 new Thread(() -> { 220 if (sha512CheckBox.isSelected()) { 221 sha512TextField.setText(calculate(CalculateType.Sha512, text)); 222 } else { 223 sha512TextField.setText(""); 224 } 225 progressBar.setValue(progressBar.getValue() + 15); 226 }).start(); 227 228 new Thread(() -> { 229 if (crc32CheckBox.isSelected()) { 230 crc32TextField.setText(calculate(CalculateType.Crc32, text)); 231 } else { 232 crc32TextField.setText(""); 233 } 234 progressBar.setValue(progressBar.getValue() + 15); 235 }).start(); 236 } 237 238 private String calculate(final CalculateType selectedHash, final String text) { 239 String result = ""; 240 try (ByteArrayInputStream bais = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8))) { 241 switch (selectedHash) { 242 case Md5: { 243 result = DigestUtils.md5Hex(bais); 244 break; 245 } 246 case Sha1: { 247 result = DigestUtils.sha1Hex(bais); 248 break; 249 } 250 case Sha256: { 251 result = DigestUtils.sha256Hex(bais); 252 break; 253 } 254 case Sha384: { 255 result = DigestUtils.sha384Hex(bais); 256 break; 257 } 258 case Sha512: { 259 result = DigestUtils.sha512Hex(bais); 260 break; 261 } 262 case Crc32: { 263 CRC32 crc32 = new CRC32(); 264 crc32.update(text.getBytes()); 265 result = Long.toHexString(crc32.getValue()); 266 break; 267 } 268 default: { 269 result = "Not support"; 270 break; 271 } 272 } 273 } catch (IOException e) { 274 logger.error("calculate, IOException"); 275 } 276 return result; 277 } 278 } 279