1package org.bouncycastle.jcajce.provider.digest; 2 3import org.bouncycastle.asn1.nist.NISTObjectIdentifiers; 4import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; 5import org.bouncycastle.crypto.CipherKeyGenerator; 6import org.bouncycastle.crypto.digests.SHA256Digest; 7import org.bouncycastle.crypto.macs.HMac; 8import org.bouncycastle.jcajce.provider.config.ConfigurableProvider; 9import org.bouncycastle.jcajce.provider.symmetric.util.BaseKeyGenerator; 10import org.bouncycastle.jcajce.provider.symmetric.util.BaseMac; 11import org.bouncycastle.jcajce.provider.symmetric.util.PBESecretKeyFactory; 12 13public class SHA256 14{ 15 private SHA256() 16 { 17 18 } 19 20 static public class Digest 21 extends BCMessageDigest 22 implements Cloneable 23 { 24 public Digest() 25 { 26 super(new SHA256Digest()); 27 } 28 29 public Object clone() 30 throws CloneNotSupportedException 31 { 32 Digest d = (Digest)super.clone(); 33 d.digest = new SHA256Digest((SHA256Digest)digest); 34 35 return d; 36 } 37 } 38 39 public static class HashMac 40 extends BaseMac 41 { 42 public HashMac() 43 { 44 super(new HMac(new SHA256Digest())); 45 } 46 } 47 48 // BEGIN android-removed 49 // /** 50 // * PBEWithHmacSHA 51 // */ 52 // public static class PBEWithMacKeyFactory 53 // extends PBESecretKeyFactory 54 // { 55 // public PBEWithMacKeyFactory() 56 // { 57 // super("PBEwithHmacSHA256", null, false, PKCS12, SHA256, 256, 0); 58 // } 59 // } 60 // END android-removed 61 62 /** 63 * HMACSHA256 64 */ 65 public static class KeyGenerator 66 extends BaseKeyGenerator 67 { 68 public KeyGenerator() 69 { 70 super("HMACSHA256", 256, new CipherKeyGenerator()); 71 } 72 } 73 74 public static class Mappings 75 extends DigestAlgorithmProvider 76 { 77 private static final String PREFIX = SHA256.class.getName(); 78 79 public Mappings() 80 { 81 } 82 83 public void configure(ConfigurableProvider provider) 84 { 85 provider.addAlgorithm("MessageDigest.SHA-256", PREFIX + "$Digest"); 86 provider.addAlgorithm("Alg.Alias.MessageDigest.SHA256", "SHA-256"); 87 provider.addAlgorithm("Alg.Alias.MessageDigest." + NISTObjectIdentifiers.id_sha256, "SHA-256"); 88 89 // BEGIN android-removed 90 // provider.addAlgorithm("SecretKeyFactory.PBEWITHHMACSHA256", PREFIX + "$PBEWithMacKeyFactory"); 91 // provider.addAlgorithm("Alg.Alias.SecretKeyFactory.PBEWITHHMACSHA-256", "PBEWITHHMACSHA256"); 92 // provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + NISTObjectIdentifiers.id_sha256, "PBEWITHHMACSHA256"); 93 // END android-removed 94 95 addHMACAlgorithm(provider, "SHA256", PREFIX + "$HashMac", PREFIX + "$KeyGenerator"); 96 addHMACAlias(provider, "SHA256", PKCSObjectIdentifiers.id_hmacWithSHA256); 97 addHMACAlias(provider, "SHA256", NISTObjectIdentifiers.id_sha256); 98 } 99 } 100} 101