1 /*
   2  * Copyright (c) 2018, 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  */
  23 
  24 import java.security.*;
  25 
  26 class DigestBase extends MessageDigestSpi {
  27 
  28     private MessageDigest digest = null;
  29 
  30     public DigestBase(String alg, String provider) throws Exception {
  31         digest = MessageDigest.getInstance(alg, provider);
  32     }
  33 
  34     @Override
  35     protected void engineUpdate(byte input) {
  36         digest.update(input);
  37     }
  38 
  39     @Override
  40     protected void engineUpdate(byte[] input, int offset, int len) {
  41         digest.update(input, offset, len);
  42     }
  43 
  44     @Override
  45     protected byte[] engineDigest() {
  46         return digest.digest();
  47     }
  48 
  49     @Override
  50     protected void engineReset() {
  51         digest.reset();
  52     }
  53 
  54     @Override
  55     protected int engineGetDigestLength() {
  56         return digest.getDigestLength();
  57     }
  58 
  59     public static final class MD5 extends DigestBase {
  60         public MD5() throws Exception {
  61             super("MD5", "SUN");
  62         }
  63     }
  64 
  65     public static final class SHA extends DigestBase {
  66         public SHA() throws Exception {
  67             super("SHA", "SUN");
  68         }
  69     }
  70 
  71     public static final class SHA256 extends DigestBase {
  72         public SHA256() throws Exception {
  73             super("SHA-256", "SUN");
  74         }
  75     }
  76 }