示例#1
0
  public static void main(String[] args) {
    SHA1 sha = new SHA1();

    byte[] dig1 = new byte[20];
    byte[] dig2 = new byte[20];
    byte[] dig3 = new byte[20];

    /*
     * We do not specify a charset name for getBytes(), since we assume that
     * the JVM's default encoder maps the _used_ ASCII characters exactly as
     * getBytes("US-ASCII") would do. (Ah, yes, too lazy to catch the
     * exception that can be thrown by getBytes("US-ASCII")). Note: This has
     * no effect on the SHA-1 implementation, this is just for the following
     * test code.
     */

    sha.update("abc".getBytes());
    sha.digest(dig1);

    sha.update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".getBytes());
    sha.digest(dig2);

    for (int i = 0; i < 1000000; i++) sha.update((byte) 'a');
    sha.digest(dig3);

    String dig1_res = toHexString(dig1);
    String dig2_res = toHexString(dig2);
    String dig3_res = toHexString(dig3);

    String dig1_ref = "A9993E364706816ABA3E25717850C26C9CD0D89D";
    String dig2_ref = "84983E441C3BD26EBAAE4AA1F95129E5E54670F1";
    String dig3_ref = "34AA973CD4C4DAA4F61EEB2BDBAD27316534016F";

    if (dig1_res.equals(dig1_ref)) System.out.println("SHA-1 Test 1 OK.");
    else System.out.println("SHA-1 Test 1 FAILED.");

    if (dig2_res.equals(dig2_ref)) System.out.println("SHA-1 Test 2 OK.");
    else System.out.println("SHA-1 Test 2 FAILED.");

    if (dig3_res.equals(dig3_ref)) System.out.println("SHA-1 Test 3 OK.");
    else System.out.println("SHA-1 Test 3 FAILED.");

    if (dig3_res.equals(dig3_ref)) System.out.println("SHA-1 Test 3 OK.");
    else System.out.println("SHA-1 Test 3 FAILED.");
  }
示例#2
0
 /**
  * To save from Attacks on Parameter Authentication one can send hash of parameters to other party
  *
  * @return byte[] of what???
  */
 @Override
 public byte[] getParametersHash() {
   byte[] parms = NativeLib.getECParameters(ecGroup);
   SHA1 sha1 = new SHA1(BLOCK_SIZE);
   sha1.update(parms, 0, parms.length);
   sha1.generate();
   byte[] digest = sha1.getDigest();
   return digest;
 }
示例#3
0
 @Override
 public boolean checkParametersHash(byte[] hash) {
   if (hash == null || hash.length != BLOCK_SIZE) return false;
   byte[] parms = NativeLib.getECParameters(ecGroup);
   SHA1 sha1 = new SHA1(BLOCK_SIZE);
   sha1.update(parms, 0, parms.length);
   sha1.generate();
   byte[] digest = sha1.getDigest();
   for (int i = 0; i < digest.length; i++) if (digest[i] != hash[i]) return false;
   return true;
 }
示例#4
0
  /**
   * Runs an integrity test.
   *
   * @return true: selftest passed / false: selftest failed
   */
  public boolean selfTest() {
    int nI;
    SHA1 tester;
    byte[] digest;

    tester = new SHA1();

    tester.update(SELFTEST_MESSAGE);
    tester.finalize();

    digest = tester.getDigest();

    for (nI = 0; nI < DIGEST_SIZE; nI++) {
      if (digest[nI] != SELFTEST_DIGEST[nI]) {
        return false;
      }
    }
    return true;
  }