Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/MessageDigest/TestSameValue.java
38811 views
/*1* Copyright (c) 2015, 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*/2223import static java.lang.System.out;2425import java.nio.ByteBuffer;26import java.security.DigestException;27import java.security.MessageDigest;28import java.util.Random;2930/**31* @test32* @bug 805037133* @summary Check md.digest(data) value whether same with digest output value34* with various update/digest methods.35* @author Kevin Liu36* @key randomness37*/3839public class TestSameValue {4041public static void main(String[] args) throws Exception {42TestSameValue test1 = new TestSameValue();43test1.run();44}4546private void run() throws Exception {4748byte[] data = new byte[6706];49MessageDigest md = null;50// Initialize input data51new Random().nextBytes(data);5253String[] providers = {54null, "SUN"55};56String[] algorithmArr = {57"SHA", "Sha", "MD5", "md5", "SHA-224", "SHA-256", "SHA-384",58"SHA-512"59};6061for (String algorithm: algorithmArr) {62for (String provider: providers) {63if (provider != null) {64md = MessageDigest.getInstance(algorithm, provider);65} else {66md = MessageDigest.getInstance(algorithm);67}68for (UpdateDigestMethod updateMethod: UpdateDigestMethod69.values()) {70byte[] output = updateMethod.updateDigest(data, md);71// Get the output and the "correct" one72byte[] standard = md.digest(data);73// Compare input and output74if (!MessageDigest.isEqual(output, standard)) {75throw new RuntimeException(76"Test failed at algorithm/provider/numUpdate:"77+ algorithm + "/" + provider + "/"78+ updateMethod);79}80}81}82}8384out.println("All " + algorithmArr.length85* UpdateDigestMethod.values().length * providers.length86+ " tests Passed");87}8889private static enum UpdateDigestMethod {9091/*92* update the data one by one using method update(byte input) then93* do digest (giving the output buffer, offset, and the number of94* bytes to put in the output buffer)95*/96UPDATE_DIGEST_BUFFER {97@Override98public byte[] updateDigest(byte[] data, MessageDigest md)99throws DigestException {100for (byte element: data) {101md.update(element);102}103byte[] output = new byte[md.getDigestLength()];104int len = md.digest(output, 0, output.length);105if (len != output.length) {106throw new RuntimeException(107"ERROR" + ": digest length differs!");108}109return output;110}111},112113/*114* update the data one by one using method update(byte input)115* then do digest116*/117UPDATE_DIGEST {118@Override119public byte[] updateDigest(byte[] data, MessageDigest md) {120for (byte element: data) {121md.update(element);122}123return md.digest();124}125},126127/*128* update all the data at once as a block, then do digest ( giving the129* output buffer, offset, and the number of bytes to put in the output130* buffer)131*/132UPDATE_BLOCK_DIGEST_BUFFER {133@Override134public byte[] updateDigest(byte[] data, MessageDigest md)135throws DigestException {136md.update(data);137byte[] output = new byte[md.getDigestLength()];138int len = md.digest(output, 0, output.length);139if (len != output.length) {140throw new RuntimeException(141"ERROR" + ": digest length differs!");142}143return output;144}145},146147// update all the data at once as a block, then do digest148UPDATE_BLOCK_DIGEST {149@Override150public byte[] updateDigest(byte[] data, MessageDigest md) {151md.update(data);152return md.digest();153}154},155156/*157* update the leading bytes (length is "data.length-LASTNBYTES")158* at once as a block, then do digest (do a final update using159* the left LASTNBYTES bytes which is passed as a parameter for160* the digest method, then complete the digest)161*/162UPDATE_LEADING_BLOCK_DIGEST_REMAIN {163@Override164public byte[] updateDigest(byte[] data, MessageDigest md) {165byte[] mainPart = new byte[data.length - LASTNBYTES];166for (int i = 0; i < mainPart.length; i++) {167mainPart[i] = data[i];168}169for (int j = 0; j < LASTNBYTES; j++) {170REMAIN[j] = data[data.length - LASTNBYTES + j];171}172md.update(mainPart);173return md.digest(REMAIN);174}175},176177/*178* update the data 2 bytes each time, after finishing updating,179* do digest (giving the output buffer, offset, and the number180* of bytes to put in the output buffer)181*/182UPDATE_BYTES_DIGEST_BUFFER {183@Override184public byte[] updateDigest(byte[] data, MessageDigest md)185throws DigestException {186187for (int i = 0; i < data.length / 2; i++) {188md.update(data, i * 2, 2);189}190byte[] output = new byte[md.getDigestLength()];191int len = md.digest(output, 0, output.length);192if (len != output.length) {193throw new RuntimeException(194"ERROR" + ": digest length differs!");195}196return output;197}198},199200/*201* update the data 2 bytes each time, after finishing updating,202* do digest203*/204UPDATE_BYTES_DIGEST {205@Override206public byte[] updateDigest(byte[] data, MessageDigest md) {207for (int i=0;i<data.length/2;i++){208md.update(data,i*2,2);209}210return md.digest();211}212},213214/*215* update the data one by one using method update(byte[] input,216* int offset, int len) for the leading bytes (length is217* "data.length-LASTNBYTES"), then do digest (do a final218* update using the left LASTNBYTES bytes which is passed219* as a parameter for digest method then complete the digest)220*/221UPDATE_BUFFER_LEADING_DIGEST_REMAIN {222@Override223public byte[] updateDigest(byte[] data, MessageDigest md) {224for (int i = 0; i < data.length - LASTNBYTES; i++) {225md.update(data, i, 1);226}227for (int j = 0; j < LASTNBYTES; j++) {228REMAIN[j] = data[data.length - LASTNBYTES + j];229}230return md.digest(REMAIN);231}232},233234/*235* update the data one by one using method update(byte input)236* for the leading bytes (length is "data.length-LASTNBYTES"),237* then do digest (do a final update using the left LASTNBYTES238* bytes which is passed as a parameter for digest method,239* then complete the digest)240*/241UPDATE_LEADING_DIGEST_REMAIN {242@Override243public byte[] updateDigest(byte[] data, MessageDigest md) {244for (int i = 0; i < data.length - LASTNBYTES; i++) {245md.update(data[i]);246}247for (int j = 0; j < LASTNBYTES; j++) {248REMAIN[j] = data[data.length - LASTNBYTES + j];249}250return md.digest(REMAIN);251}252},253254/*255* update all the data at once as a ByteBuffer, then do digest256* (giving the output buffer, offset, and the number of bytes257* to put in the output buffer)258*/259UPDATE_BYTE_BUFFER_DIGEST_BUFFER {260@Override261public byte[] updateDigest(byte[] data, MessageDigest md)262throws DigestException {263md.update(ByteBuffer.wrap(data));264byte[] output = new byte[md.getDigestLength()];265int len = md.digest(output, 0, output.length);266if (len != output.length) {267throw new RuntimeException(268"ERROR" + ": digest length differs!");269}270return output;271}272},273274// update all the data at once as a ByteBuffer, then do digest275UPDATE_BYTE_BUFFER_DIGEST {276@Override277public byte[] updateDigest(byte[] data, MessageDigest md) {278md.update(ByteBuffer.wrap(data));279return md.digest();280}281},282283/*284* update the leading bytes (length is "data.length-LASTNBYTES")285* at once as a ByteBuffer, then do digest (do a final update286* using the left LASTNBYTES bytes which is passed as a parameter287* for the digest method, then complete the digest)288*/289UPDATE_BYTE_BUFFER_LEADING_DIGEST_REMAIN {290@Override291public byte[] updateDigest(byte[] data, MessageDigest md) {292byte[] mainPart = new byte[data.length - LASTNBYTES];293for (int i = 0; i < mainPart.length; i++) {294mainPart[i] = data[i];295}296for (int j = 0; j < LASTNBYTES; j++) {297REMAIN[j] = data[data.length - LASTNBYTES + j];298}299md.update(ByteBuffer.wrap(mainPart));300return md.digest(REMAIN);301}302};303304private static final int LASTNBYTES = 5;305private static final byte[] REMAIN = new byte[LASTNBYTES];306307public abstract byte[] updateDigest(byte[] data, MessageDigest md)308throws DigestException;309}310}311312313