Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SHA-1
- private static String byteArrToString(byte[] b){
- String res = null;
- StringBuffer sb = new StringBuffer(b.length * 2);
- for (int i = 0; i < b.length; i++){
- int j = b[i] & 0xff;
- if (j < 16) {
- sb.append('0');
- }
- sb.append(Integer.toHexString(j));
- }
- res = sb.toString();
- return res.toUpperCase();
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- String passwordSHA = null;
- MessageDigest sha = null;
- try {
- sha = MessageDigest.getInstance("SHA-1");
- } catch (NoSuchAlgorithmException ex) {
- Logger.getLogger(SHA.class.getName()).log(Level.SEVERE, null, ex);
- }
- byte[] tmp = jTextArea1.getText().getBytes();
- sha.update(tmp);
- String textSHA = byteArrToString(sha.digest());
- jTextField1.setText(textSHA);
- }
- //MD5
- public static String toHex(byte [] digest) {
- StringBuffer hexString = new StringBuffer();
- for(int i = 0; i<digest.length; i++)
- {
- hexString.append(Integer.toHexString(0xFF & digest[i]));
- }
- return new String(hexString);
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- MessageDigest messageDigest = null;
- try {
- messageDigest = MessageDigest.getInstance("MD5");
- } catch (NoSuchAlgorithmException ex) {
- Logger.getLogger(MD5.class.getName()).log(Level.SEVERE, null, ex);
- }
- String text;
- text = jTextArea1.getText();
- byte[] plaintext = null;
- try {
- plaintext = text.getBytes("UTF8");
- } catch (UnsupportedEncodingException ex) {
- Logger.getLogger(MD5.class.getName()).log(Level.SEVERE, null, ex);
- }
- messageDigest.update(plaintext);
- byte[] msgDigest = messageDigest.digest();
- String digest = toHex(msgDigest);
- jTextField1.setText(digest);
- }
- //MAC
- public static String toHex(byte [] digest) {
- StringBuffer hexString = new StringBuffer();
- for(int i = 0; i<digest.length; i++)
- {
- hexString.append(Integer.toHexString(0xFF & digest[i]));
- }
- return new String(hexString);
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- KeyGenerator keyGen = null;
- try {
- keyGen = KeyGenerator.getInstance("HmacMD5");
- } catch (NoSuchAlgorithmException ex) {
- Logger.getLogger(MAC.class.getName()).log(Level.SEVERE, null, ex);
- }
- SecretKey MD5key = keyGen.generateKey();
- String text;
- text = jTextArea1.getText();
- byte[] plaintext = null;
- try {
- plaintext = text.getBytes("UTF8");
- } catch (UnsupportedEncodingException ex) {
- Logger.getLogger(MD5.class.getName()).log(Level.SEVERE, null, ex);
- }
- Mac mac = null;
- try {
- mac = Mac.getInstance("HmacMD5");
- } catch (NoSuchAlgorithmException ex) {
- Logger.getLogger(MAC.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- mac.init(MD5key);
- } catch (InvalidKeyException ex) {
- Logger.getLogger(MAC.class.getName()).log(Level.SEVERE, null, ex);
- }
- mac.update(plaintext);
- byte[] byteMacFinal = mac.doFinal();
- String macFinal = toHex(byteMacFinal);
- jTextField1.setText(macFinal);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement