Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.jcajce.provider.symmetric;
  
  
 
 
 public class PBEPBKDF2
 {
     private PBEPBKDF2()
     {
 
     }
 
     public static class AlgParams
         extends BaseAlgorithmParameters
     {
         PBKDF2Params params;
 
         protected byte[] engineGetEncoded()
         {
             try
             {
                 return .getEncoded(.);
             }
             catch (IOException e)
             {
                 throw new RuntimeException("Oooops! " + e.toString());
             }
         }
 
         protected byte[] engineGetEncoded(
             String format)
         {
             if (this.isASN1FormatString(format))
             {
                 return engineGetEncoded();
             }
 
             return null;
         }
 
             Class paramSpec)
             throws InvalidParameterSpecException
         {
             if (paramSpec == PBEParameterSpec.class)
             {
                 return new PBEParameterSpec(.getSalt(),
                                 .getIterationCount().intValue());
             }
 
             throw new InvalidParameterSpecException("unknown parameter spec passed to PBKDF2 PBE parameters object.");
         }
 
         protected void engineInit(
             AlgorithmParameterSpec paramSpec)
             throws InvalidParameterSpecException
         {
             if (!(paramSpec instanceof PBEParameterSpec))
             {
                 throw new InvalidParameterSpecException("PBEParameterSpec required to initialise a PBKDF2 PBE parameters algorithm parameters object");
             }
 
             PBEParameterSpec    pbeSpec = (PBEParameterSpec)paramSpec;
 
             this. = new PBKDF2Params(pbeSpec.getSalt(),
                                 pbeSpec.getIterationCount());
         }
 
         protected void engineInit(
             byte[] params)
             throws IOException
         {
             this. = PBKDF2Params.getInstance(ASN1Primitive.fromByteArray(params));
         }
 
         protected void engineInit(
             byte[] params,
            String format)
            throws IOException
        {
            if (this.isASN1FormatString(format))
            {
                engineInit(params);
                return;
            }
            throw new IOException("Unknown parameters format in PBKDF2 parameters object");
        }
        protected String engineToString()
        {
            return "PBKDF2 Parameters";
        }
    }
    public static class BasePBKDF2
        extends BaseSecretKeyFactory
    {
        private int scheme;
        public BasePBKDF2(String nameint scheme)
        {
            super(name.);
            this. = scheme;
        }
        protected SecretKey engineGenerateSecret(
            KeySpec keySpec)
            throws InvalidKeySpecException
        {
            if (keySpec instanceof PBEKeySpec)
            {
                PBEKeySpec pbeSpec = (PBEKeySpec)keySpec;
                if (pbeSpec.getSalt() == null)
                {
                    throw new InvalidKeySpecException("missing required salt");
                }
                if (pbeSpec.getIterationCount() <= 0)
                {
                    throw new InvalidKeySpecException("positive iteration count required: "
                        + pbeSpec.getIterationCount());
                }
                if (pbeSpec.getKeyLength() <= 0)
                {
                    throw new InvalidKeySpecException("positive key length required: "
                        + pbeSpec.getKeyLength());
                }
                if (pbeSpec.getPassword().length == 0)
                {
                    throw new IllegalArgumentException("password empty");
                }
                if (pbeSpec instanceof PBKDF2KeySpec)
                {
                    PBKDF2KeySpec spec = (PBKDF2KeySpec)pbeSpec;
                    int digest = getDigestCode(spec.getPrf().getAlgorithm());
                    int keySize = pbeSpec.getKeyLength();
                    int ivSize = -1;    // JDK 1,2 and earlier does not understand simplified version.
                    CipherParameters param = PBE.Util.makePBEMacParameters(pbeSpecdigestkeySize);
                    return new BCPBEKey(this.this.digestkeySizeivSizepbeSpecparam);
                }
                else
                {
                    int digest = ;
                    int keySize = pbeSpec.getKeyLength();
                    int ivSize = -1;    // JDK 1,2 and earlier does not understand simplified version.
                    CipherParameters param = PBE.Util.makePBEMacParameters(pbeSpecdigestkeySize);
                    return new BCPBEKey(this.this.digestkeySizeivSizepbeSpecparam);
                }
            }
            throw new InvalidKeySpecException("Invalid KeySpec");
        }
        private int getDigestCode(ASN1ObjectIdentifier algorithm)
            throws InvalidKeySpecException
        {
            if (algorithm.equals(.))
            {
                return ;
            }
            else if (algorithm.equals(.))
            {
                return ;
            }
            throw new InvalidKeySpecException("Invalid KeySpec: unknown PRF algorithm " + algorithm);
        }
    }
    public static class PBKDF2withUTF8
        extends BasePBKDF2
    {
        public PBKDF2withUTF8()
        {
            super("PBKDF2");
        }
    }
    public static class Mappings
        extends AlgorithmProvider
    {
        private static final String PREFIX = PBEPBKDF2.class.getName();
        public Mappings()
        {
        }
        public void configure(ConfigurableProvider provider)
        {
            provider.addAlgorithm("AlgorithmParameters.PBKDF2" + "$AlgParams");
            provider.addAlgorithm("Alg.Alias.AlgorithmParameters." + ."PBKDF2");
            provider.addAlgorithm("SecretKeyFactory.PBKDF2" + "$PBKDF2withUTF8");
            provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + ."PBKDF2");
        }
    }
New to GrepCode? Check out our FAQ X