Path: blob/master/test/jdk/sun/security/provider/MessageDigest/Offsets.java
41154 views
/*1* Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 642951026* @summary Verify that our digests work correctly irrespective of input alignment27* @author Andreas Sterbenz28*/2930import java.util.*;3132import java.security.*;3334public class Offsets {3536private static void outOfBounds(MessageDigest md, int arrayLen, int ofs, int len) throws Exception {37try {38md.reset();39md.update(new byte[arrayLen], ofs, len);40throw new Exception("invalid call succeeded");41} catch (RuntimeException e) {42// ignore43//System.out.println(e);44}45}4647private static void test(String algorithm, int minOfs, int maxOfs, int minLen, int maxLen) throws Exception {48Random random = new Random();49MessageDigest md = MessageDigest.getInstance(algorithm, "SUN");50System.out.println("Testing " + algorithm + "...");51outOfBounds(md, 16, 0, 32);52outOfBounds(md, 16, -8, 16);53outOfBounds(md, 16, 8, -8);54outOfBounds(md, 16, Integer.MAX_VALUE, 8);55for (int n = minLen; n <= maxLen; n++) {56System.out.print(n + " ");57byte[] data = new byte[n];58random.nextBytes(data);59byte[] digest = null;60for (int ofs = minOfs; ofs <= maxOfs; ofs++) {61byte[] ofsData = new byte[n + maxOfs];62random.nextBytes(ofsData);63System.arraycopy(data, 0, ofsData, ofs, n);64md.update(ofsData, ofs, n);65byte[] ofsDigest = md.digest();66if (digest == null) {67digest = ofsDigest;68} else {69if (Arrays.equals(digest, ofsDigest) == false) {70throw new Exception("Digest mismatch " + algorithm + ", ofs: " + ofs + ", len: " + n);71}72}73}74}75System.out.println();76}7778public static void main(String[] args) throws Exception {79test("MD2", 0, 64, 0, 128);80test("MD5", 0, 64, 0, 128);81test("SHA1", 0, 64, 0, 128);82test("SHA-224", 0, 64, 0, 128);83test("SHA-256", 0, 64, 0, 128);84test("SHA-384", 0, 128, 0, 256);85test("SHA-512", 0, 128, 0, 256);86test("SHA3-224", 0, 64, 0, 128);87test("SHA3-256", 0, 64, 0, 128);88test("SHA3-384", 0, 128, 0, 256);89test("SHA3-512", 0, 128, 0, 256);90}9192}939495