blob: 1ce00a3c7966c18116bfcb3e7d88473abbfbd0c8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package org.spongycastle.jcajce.provider.symmetric.util;
import java.lang.reflect.Constructor;
import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactorySpi;
import javax.crypto.spec.SecretKeySpec;
import org.spongycastle.asn1.ASN1ObjectIdentifier;
public class BaseSecretKeyFactory
extends SecretKeyFactorySpi
implements PBE
{
protected String algName;
protected ASN1ObjectIdentifier algOid;
protected BaseSecretKeyFactory(
String algName,
ASN1ObjectIdentifier algOid)
{
this.algName = algName;
this.algOid = algOid;
}
protected SecretKey engineGenerateSecret(
KeySpec keySpec)
throws InvalidKeySpecException
{
if (keySpec instanceof SecretKeySpec)
{
return (SecretKey)keySpec;
}
throw new InvalidKeySpecException("Invalid KeySpec");
}
protected KeySpec engineGetKeySpec(
SecretKey key,
Class keySpec)
throws InvalidKeySpecException
{
if (keySpec == null)
{
throw new InvalidKeySpecException("keySpec parameter is null");
}
if (key == null)
{
throw new InvalidKeySpecException("key parameter is null");
}
if (SecretKeySpec.class.isAssignableFrom(keySpec))
{
return new SecretKeySpec(key.getEncoded(), algName);
}
try
{
Class[] parameters = { byte[].class };
Constructor c = keySpec.getConstructor(parameters);
Object[] p = new Object[1];
p[0] = key.getEncoded();
return (KeySpec)c.newInstance(p);
}
catch (Exception e)
{
throw new InvalidKeySpecException(e.toString());
}
}
protected SecretKey engineTranslateKey(
SecretKey key)
throws InvalidKeyException
{
if (key == null)
{
throw new InvalidKeyException("key parameter is null");
}
if (!key.getAlgorithm().equalsIgnoreCase(algName))
{
throw new InvalidKeyException("Key not of type " + algName + ".");
}
return new SecretKeySpec(key.getEncoded(), algName);
}
}
|