Java Tutorial/Security/MessageDigest — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 05:01, 1 июня 2010

Create an encrypted string for password

import java.security.MessageDigest;
public class Main {
  public static void main(String[] args) throws Exception {
    String password = "secret";
    String algorithm = "SHA";
    byte[] plainText = password.getBytes();
    MessageDigest md = MessageDigest.getInstance(algorithm);
    md.reset();
    md.update(plainText);
    byte[] encodedPassword = md.digest();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < encodedPassword.length; i++) {
      if ((encodedPassword[i] & 0xff) < 0x10) {
        sb.append("0");
      }
      sb.append(Long.toString(encodedPassword[i] & 0xff, 16));
    }
    System.out.println("Plain    : " + password);
    System.out.println("Encrypted: " + sb.toString());
  }
}





Cryptography Streams: URLDigest

import java.io.InputStream;
import java.math.BigInteger;
import java.net.URL;
import java.security.MessageDigest;
import java.util.Arrays;
public class Main {
  public static void main(String[] args) throws Exception {
    URL u = new URL("http://www.google.ru");
    InputStream in = u.openStream();
    MessageDigest sha = MessageDigest.getInstance("SHA");
    byte[] data = new byte[1024];
    int bytesRead = -1;
    while ((bytesRead = in.read(data)) >= 0) {
      sha.update(data, 0, bytesRead);
    }
    byte[] result = sha.digest();
    System.out.println(Arrays.toString(result));
    System.out.println(new BigInteger(result));
  }
}





Digest Input

import java.io.FileInputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
public class MainClass {
  public static void main(String args[]) throws Exception {
    MessageDigest m = MessageDigest.getInstance("MD5");
    FileInputStream fin = new FileInputStream(args[0]);
    DigestInputStream din = new DigestInputStream(fin, m);
    while (din.read() != -1)
      ;
    byte s[] = m.digest();
    for (int i = 0; i < s.length; i++) {
      System.out.print( Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6));
    }
  }
}





Digest Stream

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.DigestInputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
public class MainClass {
  public static void main(String[] args) throws Exception{
    performOutputTest();
    performInputTest();
  }
  static void performOutputTest() throws Exception {
      MessageDigest md = MessageDigest.getInstance("SHA");
      FileOutputStream fout = new FileOutputStream("sha-results.txt");
      DigestOutputStream out = new DigestOutputStream(fout, md);
      byte[] b = "testCase".getBytes();
      out.write(b, 0, b.length);
      md = out.getMessageDigest();
      String s = new String(md.digest());
      System.out.println("Calculated result: " + s);
  }
  static void performInputTest()  throws Exception{
      MessageDigest md = MessageDigest.getInstance("SHA");
      FileInputStream fin = new FileInputStream("sha-results.txt");
      DigestInputStream in = new DigestInputStream(fin, md);
      byte[] b = new byte["testCase".getBytes().length];
      in.read(b, 0, "testCase".getBytes().length);
      md = in.getMessageDigest();
      String s = new String(md.digest());
      System.out.println("Calculated result:  " + s);
  }
}





extends MessageDigest

import java.security.MessageDigest;
public class XYZMessageDigest extends MessageDigest implements Cloneable {
  private int hash;
  private int store;
  private int nBytes;
  public XYZMessageDigest() {
    super("XYZ");
    engineReset();
  }
  public void engineUpdate(byte b) {
    switch (nBytes) {
    case 0:
      store = (b << 24) & 0xff000000;
      break;
    case 1:
      store |= (b << 16) & 0x00ff0000;
      break;
    case 2:
      store |= (b << 8) & 0x0000ff00;
      break;
    case 3:
      store |= (b << 0) & 0x000000ff;
      break;
    }
    nBytes++;
    if (nBytes == 4) {
      hash = hash ^ store;
      nBytes = 0;
      store = 0;
    }
  }
  public void engineUpdate(byte b[], int offset, int length) {
    for (int i = 0; i < length; i++)
      engineUpdate(b[i + offset]);
  }
  public void engineReset() {
    hash = 0;
    store = 0;
    nBytes = 0;
  }
  public byte[] engineDigest() {
    while (nBytes != 0)
      engineUpdate((byte) 0);
    byte b[] = new byte[4];
    b[0] = (byte) (hash >>> 24);
    b[1] = (byte) (hash >>> 16);
    b[2] = (byte) (hash >>> 8);
    b[3] = (byte) (hash >>> 0);
    engineReset();
    return b;
  }
}





Make SHA Message

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MainClass {
  public static byte[] makeDigest(byte[] mush, long t2, double q2) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(mush);
    md.update(makeBytes(t2, q2));
    return md.digest();
  }
}





Make SHA Message with update methods

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MainClass {
  public static byte[] makeDigest(String user, String password, long t1, double q1)
      throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(user.getBytes());
    md.update(password.getBytes());
    md.update(makeBytes(t1, q1));
    return md.digest();
  }
}





Masher a file

import java.io.FileInputStream;
import java.security.MessageDigest;
import sun.misc.BASE64Encoder;
public class MainClass {
  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.out.println("Usage: Masher filename");
      return;
    }
    MessageDigest md = MessageDigest.getInstance("MD5");
    FileInputStream in = new FileInputStream(args[0]);
    byte[] buffer = new byte[8192];
    int length;
    while ((length = in.read(buffer)) != -1)
      md.update(buffer, 0, length);
    byte[] raw = md.digest();
    BASE64Encoder encoder = new BASE64Encoder();
    String base64 = encoder.encode(raw);
    System.out.println(base64);
  }
}





MD5 MessageDigest

import java.security.MessageDigest;
public class MainClass {
  public static void main(String args[]) throws Exception {
    MessageDigest m = MessageDigest.getInstance("MD5");
    m.update("test".getBytes());
    byte s[] = m.digest();
    String result = "";
    for (int i = 0; i < s.length; i++) {
      result += Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6);
    }
    System.out.println(result);
  }
}
//098f6bcd4621d373cade4e832627b4f6





Message Digest Demo

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
public class MainClass {
  private static int BUFFER_SIZE = 32 * 1024;
  public static void main(String[] args) throws Exception {
    System.out.println("test.txt: " + md("text.txt"));
  }
  public static String md(String f) throws Exception {
    BufferedInputStream file = new BufferedInputStream(new FileInputStream(f));
    MessageDigest md = MessageDigest.getInstance("MD5");
    DigestInputStream in = new DigestInputStream(file, md);
    int i;
    byte[] buffer = new byte[BUFFER_SIZE];
    do {
      i = in.read(buffer, 0, BUFFER_SIZE);
    } while (i == BUFFER_SIZE);
    md = in.getMessageDigest();
    in.close();
    return new String(md.digest());
  }
}