< prev index next >

test/jdk/java/security/MessageDigest/TestSameValue.java

Print this page
rev 59383 : [mq]: final
   1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   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  */


  43 
  44 public class TestSameValue {
  45 
  46     public static void main(String[] args) throws Exception {
  47         TestSameValue test1 = new TestSameValue();
  48         test1.run();
  49     }
  50 
  51     private void run() throws Exception {
  52 
  53         byte[] data = new byte[6706];
  54         MessageDigest md = null;
  55         // Initialize input data
  56         RandomFactory.getRandom().nextBytes(data);
  57 
  58         String[] algorithmArr = { "SHA", "Sha", "MD5", "md5", "SHA-224",
  59                 "SHA-256", "SHA-384", "SHA-512", "SHA3-224", "SHA3-256",
  60                 "SHA3-384", "SHA3-512" };
  61 
  62         for (String algorithm : algorithmArr) {
  63             try {
  64                 md = MessageDigest.getInstance(algorithm);
  65 
  66                 for (UpdateDigestMethod updateMethod : UpdateDigestMethod
  67                         .values()) {
  68                     byte[] output = updateMethod.updateDigest(data, md);
  69                     // Get the output and the "correct" one
  70                     byte[] standard = md.digest(data);
  71                     // Compare input and output
  72                     if (!MessageDigest.isEqual(output, standard)) {
  73                         throw new RuntimeException(
  74                                 "Test failed at algorithm/provider/numUpdate:"
  75                                         + algorithm + "/" + md.getProvider()
  76                                         + "/" + updateMethod);
  77                     }
  78                 }
  79             } catch (NoSuchAlgorithmException nae) {
  80                 if (algorithm.startsWith("SHA3") && !isSHA3supported()) {
  81                     continue;
  82                 } else {
  83                     throw nae;
  84                 }
  85             }
  86         }
  87 
  88         out.println("All "
  89                 + algorithmArr.length * UpdateDigestMethod.values().length
  90                 + " tests Passed");
  91     }
  92 
  93     // SHA-3 hash algorithms are only supported by "SUN" provider
  94     // and "OracleUcrypto" provider on Solaris 12.0 or later
  95     // This method checks if system supports SHA-3
  96     private boolean isSHA3supported() {
  97         if (Security.getProvider("SUN") != null) {
  98             return true;
  99         }
 100         if (Security.getProvider("OracleUcrypto") != null
 101                 && "SunOS".equals(System.getProperty("os.name"))
 102                 && System.getProperty("os.version").compareTo("5.12") >= 0) {
 103             return true;
 104         }
 105         return false;
 106     }
 107 
 108     private static enum UpdateDigestMethod {
 109 
 110         /*
 111          * update the data one by one using method update(byte input) then do
 112          * digest (giving the output buffer, offset, and the number of bytes to
 113          * put in the output buffer)
 114          */
 115         UPDATE_DIGEST_BUFFER {
 116             @Override
 117             public byte[] updateDigest(byte[] data, MessageDigest md)
 118                     throws DigestException {
 119                 for (byte element : data) {
 120                     md.update(element);
 121                 }
 122                 byte[] output = new byte[md.getDigestLength()];
 123                 int len = md.digest(output, 0, output.length);
 124                 if (len != output.length) {
 125                     throw new RuntimeException(


   1 /*
   2  * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
   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  */


  43 
  44 public class TestSameValue {
  45 
  46     public static void main(String[] args) throws Exception {
  47         TestSameValue test1 = new TestSameValue();
  48         test1.run();
  49     }
  50 
  51     private void run() throws Exception {
  52 
  53         byte[] data = new byte[6706];
  54         MessageDigest md = null;
  55         // Initialize input data
  56         RandomFactory.getRandom().nextBytes(data);
  57 
  58         String[] algorithmArr = { "SHA", "Sha", "MD5", "md5", "SHA-224",
  59                 "SHA-256", "SHA-384", "SHA-512", "SHA3-224", "SHA3-256",
  60                 "SHA3-384", "SHA3-512" };
  61 
  62         for (String algorithm : algorithmArr) {

  63             md = MessageDigest.getInstance(algorithm);
  64 
  65             for (UpdateDigestMethod updateMethod : UpdateDigestMethod
  66                      .values()) {
  67                 byte[] output = updateMethod.updateDigest(data, md);
  68                 // Get the output and the "correct" one
  69                 byte[] standard = md.digest(data);
  70                 // Compare input and output
  71                 if (!MessageDigest.isEqual(output, standard)) {
  72                     throw new RuntimeException(
  73                             "Test failed at algorithm/provider/numUpdate:"
  74                                     + algorithm + "/" + md.getProvider()
  75                                     + "/" + updateMethod);
  76                 }
  77             }







  78         }
  79 
  80         out.println("All "
  81                 + algorithmArr.length * UpdateDigestMethod.values().length
  82                 + " tests Passed");















  83     }
  84 
  85     private static enum UpdateDigestMethod {
  86 
  87         /*
  88          * update the data one by one using method update(byte input) then do
  89          * digest (giving the output buffer, offset, and the number of bytes to
  90          * put in the output buffer)
  91          */
  92         UPDATE_DIGEST_BUFFER {
  93             @Override
  94             public byte[] updateDigest(byte[] data, MessageDigest md)
  95                     throws DigestException {
  96                 for (byte element : data) {
  97                     md.update(element);
  98                 }
  99                 byte[] output = new byte[md.getDigestLength()];
 100                 int len = md.digest(output, 0, output.length);
 101                 if (len != output.length) {
 102                     throw new RuntimeException(


< prev index next >