< prev index next >

test/jdk/java/security/MessageDigest/TestSameLength.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  */


  72                 + " tests Passed");
  73     }
  74 
  75     private boolean runTest(String algo, long dataLen, UpdateMethod whichUpdate)
  76             throws Exception {
  77         try {
  78             // Do initialization
  79             byte[] data = new byte[(int) dataLen];
  80             RandomFactory.getRandom().nextBytes(data);
  81             MessageDigest md = MessageDigest.getInstance(algo);
  82             int outputLen = md.getDigestLength();
  83 
  84             // Perform the update using all available/possible update methods
  85             whichUpdate.updateDigest(data, md, dataLen);
  86             // Get the output
  87             byte[] output = md.digest();
  88 
  89             // Compare input and output
  90             return outputLen == output.length;
  91         } catch (NoSuchAlgorithmException nae) {
  92             if (algo.startsWith("SHA3") && !isSHA3supported()) {
  93                 return true;
  94             }
  95             throw nae;
  96         } catch (Exception ex) {
  97             System.err.println("Testing: " + algo + "/" + dataLen + "/"
  98                     + whichUpdate.toString()
  99                     + " failed with unexpected exception");
 100             ex.printStackTrace();
 101             throw ex;
 102         }
 103     }
 104 
 105     // SHA-3 hash algorithms are only supported by "SUN" provider
 106     // and "OracleUcrypto" provider on Solaris 12.0 or later
 107     // This method checks if system supports SHA-3
 108     private boolean isSHA3supported() {
 109         if (Security.getProvider("SUN") != null) {
 110             return true;
 111         }
 112         if (Security.getProvider("OracleUcrypto") != null
 113                 && "SunOS".equals(System.getProperty("os.name"))
 114                 && System.getProperty("os.version").compareTo("5.12") >= 0) {
 115             return true;
 116         }
 117         return false;
 118     }
 119 
 120     private static enum UpdateMethod {
 121         UPDATE_BYTE {
 122             @Override
 123             public void updateDigest(byte[] data, MessageDigest md,
 124                     long dataLen) {
 125 
 126                 for (int i = 0; i < dataLen; i++) {
 127                     md.update(data[i]);
 128                 }
 129             }
 130         },
 131 
 132         UPDATE_BUFFER {
 133             @Override
 134             public void updateDigest(byte[] data, MessageDigest md,
 135                     long dataLen) {
 136 
 137                 md.update(data);


   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  */


  72                 + " tests Passed");
  73     }
  74 
  75     private boolean runTest(String algo, long dataLen, UpdateMethod whichUpdate)
  76             throws Exception {
  77         try {
  78             // Do initialization
  79             byte[] data = new byte[(int) dataLen];
  80             RandomFactory.getRandom().nextBytes(data);
  81             MessageDigest md = MessageDigest.getInstance(algo);
  82             int outputLen = md.getDigestLength();
  83 
  84             // Perform the update using all available/possible update methods
  85             whichUpdate.updateDigest(data, md, dataLen);
  86             // Get the output
  87             byte[] output = md.digest();
  88 
  89             // Compare input and output
  90             return outputLen == output.length;
  91         } catch (NoSuchAlgorithmException nae) {



  92             throw nae;
  93         } catch (Exception ex) {
  94             System.err.println("Testing: " + algo + "/" + dataLen + "/"
  95                     + whichUpdate.toString()
  96                     + " failed with unexpected exception");
  97             ex.printStackTrace();
  98             throw ex;
  99         }















 100     }
 101 
 102     private static enum UpdateMethod {
 103         UPDATE_BYTE {
 104             @Override
 105             public void updateDigest(byte[] data, MessageDigest md,
 106                     long dataLen) {
 107 
 108                 for (int i = 0; i < dataLen; i++) {
 109                     md.update(data[i]);
 110                 }
 111             }
 112         },
 113 
 114         UPDATE_BUFFER {
 115             @Override
 116             public void updateDigest(byte[] data, MessageDigest md,
 117                     long dataLen) {
 118 
 119                 md.update(data);


< prev index next >