jdk/src/share/classes/java/security/MessageDigest.java
changeset 4208 e14d48eee341
parent 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
4207:a9ca0ff33862 4208:e14d48eee341
     1 /*
     1 /*
     2  * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 1996-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     7  * published by the Free Software Foundation.  Sun designates this
   412      *
   412      *
   413      * @param digestb the other digest to compare.
   413      * @param digestb the other digest to compare.
   414      *
   414      *
   415      * @return true if the digests are equal, false otherwise.
   415      * @return true if the digests are equal, false otherwise.
   416      */
   416      */
   417     public static boolean isEqual(byte digesta[], byte digestb[]) {
   417     public static boolean isEqual(byte[] digesta, byte[] digestb) {
   418         if (digesta.length != digestb.length)
   418         if (digesta.length != digestb.length) {
   419             return false;
   419             return false;
   420 
   420         }
       
   421 
       
   422         int result = 0;
       
   423         // time-constant comparison
   421         for (int i = 0; i < digesta.length; i++) {
   424         for (int i = 0; i < digesta.length; i++) {
   422             if (digesta[i] != digestb[i]) {
   425             result |= digesta[i] ^ digestb[i];
   423                 return false;
   426         }
   424             }
   427         return result == 0;
   425         }
       
   426         return true;
       
   427     }
   428     }
   428 
   429 
   429     /**
   430     /**
   430      * Resets the digest for further use.
   431      * Resets the digest for further use.
   431      */
   432      */