jdk/test/sun/security/provider/MessageDigest/Offsets.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 6429510
       
    27  * @summary Verify that our digests work correctly irrespective of input alignment
       
    28  * @author Andreas Sterbenz
       
    29  */
       
    30 
       
    31 import java.util.*;
       
    32 
       
    33 import java.security.*;
       
    34 
       
    35 public class Offsets {
       
    36 
       
    37     private static void outOfBounds(MessageDigest md, int arrayLen, int ofs, int len) throws Exception {
       
    38         try {
       
    39             md.reset();
       
    40             md.update(new byte[arrayLen], ofs, len);
       
    41             throw new Exception("invalid call succeeded");
       
    42         } catch (RuntimeException e) {
       
    43             // ignore
       
    44             //System.out.println(e);
       
    45         }
       
    46     }
       
    47 
       
    48     private static void test(String algorithm, int minOfs, int maxOfs, int minLen, int maxLen) throws Exception {
       
    49         Random random = new Random();
       
    50         MessageDigest md = MessageDigest.getInstance(algorithm, "SUN");
       
    51         System.out.println("Testing " + algorithm + "...");
       
    52         outOfBounds(md, 16, 0, 32);
       
    53         outOfBounds(md, 16, -8, 16);
       
    54         outOfBounds(md, 16, 8, -8);
       
    55         outOfBounds(md, 16, Integer.MAX_VALUE, 8);
       
    56         for (int n = minLen; n <= maxLen; n++) {
       
    57             System.out.print(n + " ");
       
    58             byte[] data = new byte[n];
       
    59             random.nextBytes(data);
       
    60             byte[] digest = null;
       
    61             for (int ofs = minOfs; ofs <= maxOfs; ofs++) {
       
    62                 byte[] ofsData = new byte[n + maxOfs];
       
    63                 random.nextBytes(ofsData);
       
    64                 System.arraycopy(data, 0, ofsData, ofs, n);
       
    65                 md.update(ofsData, ofs, n);
       
    66                 byte[] ofsDigest = md.digest();
       
    67                 if (digest == null) {
       
    68                     digest = ofsDigest;
       
    69                 } else {
       
    70                     if (Arrays.equals(digest, ofsDigest) == false) {
       
    71                         throw new Exception("Digest mismatch " + algorithm + ", ofs: " + ofs + ", len: " + n);
       
    72                     }
       
    73                 }
       
    74             }
       
    75         }
       
    76         System.out.println();
       
    77     }
       
    78 
       
    79     public static void main(String[] args) throws Exception {
       
    80         test("MD2", 0, 64, 0, 128);
       
    81         test("MD5", 0, 64, 0, 128);
       
    82         test("SHA1", 0, 64, 0, 128);
       
    83         test("SHA-256", 0, 64, 0, 128);
       
    84         test("SHA-384", 0, 128, 0, 256);
       
    85         test("SHA-512", 0, 128, 0, 256);
       
    86     }
       
    87 
       
    88 }