1 /*
   2  * Copyright (c) 2014, Red Hat, Inc.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.lang.reflect.Method;
  25 
  26 import java.security.MessageDigest;
  27 import java.security.StandardMessageDigests;
  28 
  29 /**
  30  * @test
  31  * @bug 
  32  * @summary Test the StandardMessageDigests class
  33  */
  34 public class TestVectors {
  35 
  36     private static final String[] names = {
  37         "SHA1", "SHA256", "SHA384", "SHA512"
  38     };
  39 
  40     // Digests of the empty string.
  41     private static final String[] emptyDigests = {
  42         "da39a3ee5e6b4b0d3255bfef95601890afd80709",
  43         "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
  44         "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b",
  45         "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
  46     };
  47 
  48     // Digests of the string "abcd".
  49     private static final String[] abcdDigests = {
  50         "81fe8bfe87576c3ecb22426f8e57847382917acf",
  51         "88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589",
  52         "1165b3406ff0b52a3d24721f785462ca2276c9f454a116c2b2ba20171a7905ea5a026682eb659c4d5f115c363aa3c79b",
  53         "d8022f2060ad6efd297ab73dcc5355c9b214054b0d1776a136a669d26a7d3b14f73aa0d0ebff19ee333368f0164b6419a96da49e3e481753e7e96b716bdccb6f"
  54 
  55     };
  56 
  57     private static String base16(byte[] blob) {
  58         StringBuilder sb = new StringBuilder();
  59         for (byte b : blob) {
  60             char high = Character.forDigit((b >> 4) & 0xF, 16);
  61             sb.append(high);
  62             char low = Character.forDigit(b & 0xF, 16);
  63             sb.append(low);
  64         }
  65         return sb.toString();
  66     }
  67 
  68     private static MessageDigest[] createDigests() throws Exception {
  69         MessageDigest[] digests = new MessageDigest[names.length];
  70         Class<?> cls = StandardMessageDigests.class;
  71         for (int i = 0; i < digests.length; ++i) {
  72             Method method = cls.getMethod("new" + names[i]);
  73             digests[i] = (MessageDigest) method.invoke(null);
  74         }
  75         return digests;
  76     }
  77 
  78     public static void main(String[] args) throws Exception {
  79         MessageDigest[] digests1 = createDigests();
  80         for (int i = 0; i < digests1.length; ++i) {
  81             digests1[i].update((byte) 'a');
  82             digests1[i].update((byte) 'b');
  83         }
  84 
  85         // Check independence of returned objects for the same digest
  86         // algorithm.
  87         MessageDigest[] digests2 = createDigests();
  88         for (int i = 0; i < digests2.length; ++i) {
  89             String result = base16(digests2[i].digest());
  90             if (!result.equals(emptyDigests[i]))
  91                 throw new AssertionError("empty " + names[i] + ' ' + result);
  92         }
  93 
  94         // Check independence of the objects among themselves.
  95         for (int i = 0; i < digests1.length; ++i) {
  96             digests1[i].update(new byte[] {'c', 'd'});
  97         }
  98         for (int i = 0; i < digests1.length; ++i) {
  99             String result = base16(digests1[i].digest());
 100             if (!result.equals(abcdDigests[i]))
 101                 throw new AssertionError("abcd " + names[i] + ' ' + result);
 102         }
 103     }
 104 }