下面列出了java.security.spec.MGF1ParameterSpec#SHA1() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initEncodeCipher(Cipher cipher, String alias, KeyStore keyStore)
throws PFSecurityException {
try {
final PublicKey key = keyStore.getCertificate(alias).getPublicKey();
final PublicKey unrestricted = KeyFactory.getInstance(key.getAlgorithm()).generatePublic(
new X509EncodedKeySpec(key.getEncoded()));
final OAEPParameterSpec spec = new OAEPParameterSpec("SHA-256", "MGF1",
MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);
cipher.init(Cipher.ENCRYPT_MODE, unrestricted, spec);
} catch (KeyStoreException | InvalidKeySpecException |
NoSuchAlgorithmException | InvalidKeyException |
InvalidAlgorithmParameterException e) {
throw new PFSecurityException(
"Can not initialize Encode Cipher:" + e.getMessage(),
PFSecurityUtilsErrorCodes.ERROR_INIT_ENDECODE_CIPHER
);
}
}
private void initEncodeCipher(String keyAlias, int mode) throws GeneralSecurityException {
PublicKey key = mKeyStore.getCertificate(keyAlias).getPublicKey();
// workaround for using public key
// from https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.html#known-issues
PublicKey unrestricted = KeyFactory.getInstance(key.getAlgorithm())
.generatePublic(new X509EncodedKeySpec(key.getEncoded()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// from https://code.google.com/p/android/issues/detail?id=197719
OAEPParameterSpec spec = new OAEPParameterSpec("SHA-256", "MGF1",
MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);
mCipher.init(mode, unrestricted, spec);
} else {
mCipher.init(mode, unrestricted);
}
}
@Override
public AlgorithmParameterSpec buildCipherAlgorithmParameterSpec() {
return new OAEPParameterSpec(
MESSAGE_DIGEST_ALGORITHM_NAME,
MASK_GENERATION_FUNCTION_ALGORITHM_NAME,
MGF1ParameterSpec.SHA1,
PSource.PSpecified.DEFAULT);
}
/**
* getDigestAlgorithm() method testing.
*/
public void testGetDigestAlgorithm() {
String mdName = "SHA-1";
String mgfName = "MGF1";
AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
PSource pSrc = PSource.PSpecified.DEFAULT;
OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName,
mgfSpec, pSrc);
assertTrue("The returned value does not equal to the "
+ "value specified in the constructor.",
ps.getDigestAlgorithm().equals(mdName));
}
/**
* getMGFAlgorithm() method testing.
*/
public void testGetMGFAlgorithm() {
String mdName = "SHA-1";
String mgfName = "MGF1";
AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
PSource pSrc = PSource.PSpecified.DEFAULT;
OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName,
mgfSpec, pSrc);
assertTrue("The returned value does not equal to the "
+ "value specified in the constructor.",
ps.getMGFAlgorithm().equals(mgfName));
}
/**
* getMGFParameters() method testing.
*/
public void testGetMGFParameters() {
String mdName = "SHA-1";
String mgfName = "MGF1";
AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
PSource pSrc = PSource.PSpecified.DEFAULT;
OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName,
mgfSpec, pSrc);
assertTrue("The returned value does not equal to the "
+ "value specified in the constructor.",
ps.getMGFParameters() == mgfSpec);
}
/**
* getPSource() method testing.
*/
public void testGetPSource() {
String mdName = "SHA-1";
String mgfName = "MGF1";
AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
PSource pSrc = PSource.PSpecified.DEFAULT;
OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName,
mgfSpec, pSrc);
assertTrue("The returned value does not equal to the "
+ "value specified in the constructor.",
ps.getPSource() == pSrc);
}
@Override
protected void engineInit(byte[] encoded) throws IOException {
// first initialize with the DEFAULT values before
// retrieving from the encoding bytes
String mdName = DEFAULT.getDigestAlgorithm();
MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec) DEFAULT.getMGFParameters();
int saltLength = DEFAULT.getSaltLength();
int trailerField = DEFAULT.getTrailerField();
DerInputStream der = new DerInputStream(encoded);
DerValue[] datum = der.getSequence(4);
for (DerValue d : datum) {
if (d.isContextSpecific((byte) 0x00)) {
// hash algid
mdName = AlgorithmId.parse
(d.data.getDerValue()).getName();
} else if (d.isContextSpecific((byte) 0x01)) {
// mgf algid
AlgorithmId val = AlgorithmId.parse(d.data.getDerValue());
if (!val.getOID().equals(AlgorithmId.mgf1_oid)) {
throw new IOException("Only MGF1 mgf is supported");
}
AlgorithmId params = AlgorithmId.parse(
new DerValue(val.getEncodedParams()));
String mgfDigestName = params.getName();
switch (mgfDigestName) {
case "SHA-1":
mgfSpec = MGF1ParameterSpec.SHA1;
break;
case "SHA-224":
mgfSpec = MGF1ParameterSpec.SHA224;
break;
case "SHA-256":
mgfSpec = MGF1ParameterSpec.SHA256;
break;
case "SHA-384":
mgfSpec = MGF1ParameterSpec.SHA384;
break;
case "SHA-512":
mgfSpec = MGF1ParameterSpec.SHA512;
break;
case "SHA-512/224":
mgfSpec = MGF1ParameterSpec.SHA512_224;
break;
case "SHA-512/256":
mgfSpec = MGF1ParameterSpec.SHA512_256;
break;
default:
throw new IOException
("Unrecognized message digest algorithm " +
mgfDigestName);
}
} else if (d.isContextSpecific((byte) 0x02)) {
// salt length
saltLength = d.data.getDerValue().getInteger();
if (saltLength < 0) {
throw new IOException("Negative value for saltLength");
}
} else if (d.isContextSpecific((byte) 0x03)) {
// trailer field
trailerField = d.data.getDerValue().getInteger();
if (trailerField != 1) {
throw new IOException("Unsupported trailerField value " +
trailerField);
}
} else {
throw new IOException("Invalid encoded PSSParameters");
}
}
this.spec = new PSSParameterSpec(mdName, "MGF1", mgfSpec,
saltLength, trailerField);
}
@Override
protected void engineInit(byte[] encoded) throws IOException {
// first initialize with the DEFAULT values before
// retrieving from the encoding bytes
String mdName = DEFAULT.getDigestAlgorithm();
MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec) DEFAULT.getMGFParameters();
int saltLength = DEFAULT.getSaltLength();
int trailerField = DEFAULT.getTrailerField();
DerInputStream der = new DerInputStream(encoded);
DerValue[] datum = der.getSequence(4);
for (DerValue d : datum) {
if (d.isContextSpecific((byte) 0x00)) {
// hash algid
mdName = AlgorithmId.parse
(d.data.getDerValue()).getName();
} else if (d.isContextSpecific((byte) 0x01)) {
// mgf algid
AlgorithmId val = AlgorithmId.parse(d.data.getDerValue());
if (!val.getOID().equals(AlgorithmId.mgf1_oid)) {
throw new IOException("Only MGF1 mgf is supported");
}
AlgorithmId params = AlgorithmId.parse(
new DerValue(val.getEncodedParams()));
String mgfDigestName = params.getName();
switch (mgfDigestName) {
case "SHA-1":
mgfSpec = MGF1ParameterSpec.SHA1;
break;
case "SHA-224":
mgfSpec = MGF1ParameterSpec.SHA224;
break;
case "SHA-256":
mgfSpec = MGF1ParameterSpec.SHA256;
break;
case "SHA-384":
mgfSpec = MGF1ParameterSpec.SHA384;
break;
case "SHA-512":
mgfSpec = MGF1ParameterSpec.SHA512;
break;
case "SHA-512/224":
mgfSpec = MGF1ParameterSpec.SHA512_224;
break;
case "SHA-512/256":
mgfSpec = MGF1ParameterSpec.SHA512_256;
break;
default:
throw new IOException
("Unrecognized message digest algorithm " +
mgfDigestName);
}
} else if (d.isContextSpecific((byte) 0x02)) {
// salt length
saltLength = d.data.getDerValue().getInteger();
if (saltLength < 0) {
throw new IOException("Negative value for saltLength");
}
} else if (d.isContextSpecific((byte) 0x03)) {
// trailer field
trailerField = d.data.getDerValue().getInteger();
if (trailerField != 1) {
throw new IOException("Unsupported trailerField value " +
trailerField);
}
} else {
throw new IOException("Invalid encoded PSSParameters");
}
}
this.spec = new PSSParameterSpec(mdName, "MGF1", mgfSpec,
saltLength, trailerField);
}
@Override
protected void engineInit(byte[] encoded) throws IOException {
// first initialize with the DEFAULT values before
// retrieving from the encoding bytes
String mdName = DEFAULT.getDigestAlgorithm();
MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec) DEFAULT.getMGFParameters();
int saltLength = DEFAULT.getSaltLength();
int trailerField = DEFAULT.getTrailerField();
DerInputStream der = new DerInputStream(encoded);
DerValue[] datum = der.getSequence(4);
for (DerValue d : datum) {
if (d.isContextSpecific((byte) 0x00)) {
// hash algid
mdName = AlgorithmId.parse
(d.data.getDerValue()).getName();
} else if (d.isContextSpecific((byte) 0x01)) {
// mgf algid
AlgorithmId val = AlgorithmId.parse(d.data.getDerValue());
if (!val.getOID().equals(AlgorithmId.mgf1_oid)) {
throw new IOException("Only MGF1 mgf is supported");
}
AlgorithmId params = AlgorithmId.parse(
new DerValue(val.getEncodedParams()));
String mgfDigestName = params.getName();
switch (mgfDigestName) {
case "SHA-1":
mgfSpec = MGF1ParameterSpec.SHA1;
break;
case "SHA-224":
mgfSpec = MGF1ParameterSpec.SHA224;
break;
case "SHA-256":
mgfSpec = MGF1ParameterSpec.SHA256;
break;
case "SHA-384":
mgfSpec = MGF1ParameterSpec.SHA384;
break;
case "SHA-512":
mgfSpec = MGF1ParameterSpec.SHA512;
break;
case "SHA-512/224":
mgfSpec = MGF1ParameterSpec.SHA512_224;
break;
case "SHA-512/256":
mgfSpec = MGF1ParameterSpec.SHA512_256;
break;
default:
throw new IOException
("Unrecognized message digest algorithm " +
mgfDigestName);
}
} else if (d.isContextSpecific((byte) 0x02)) {
// salt length
saltLength = d.data.getDerValue().getInteger();
if (saltLength < 0) {
throw new IOException("Negative value for saltLength");
}
} else if (d.isContextSpecific((byte) 0x03)) {
// trailer field
trailerField = d.data.getDerValue().getInteger();
if (trailerField != 1) {
throw new IOException("Unsupported trailerField value " +
trailerField);
}
} else {
throw new IOException("Invalid encoded PSSParameters");
}
}
this.spec = new PSSParameterSpec(mdName, "MGF1", mgfSpec,
saltLength, trailerField);
}
RsaJceKeyCipher(PublicKey wrappingKey, PrivateKey unwrappingKey, String transformation) {
super(wrappingKey, unwrappingKey);
final Matcher matcher = SUPPORTED_TRANSFORMATIONS.matcher(transformation);
if (matcher.matches()) {
final String hashUnknownCase = matcher.group(1);
if (hashUnknownCase != null) {
// OAEP mode a.k.a PKCS #1v2
final String hash = hashUnknownCase.toUpperCase();
transformation_ = "RSA/ECB/OAEPPadding";
final MGF1ParameterSpec mgf1Spec;
switch (hash) {
case "SHA-1":
mgf1Spec = MGF1ParameterSpec.SHA1;
break;
case "SHA-224":
LOGGER.warning(transformation + " is not officially supported by the JceMasterKey");
mgf1Spec = MGF1ParameterSpec.SHA224;
break;
case "SHA-256":
mgf1Spec = MGF1ParameterSpec.SHA256;
break;
case "SHA-384":
mgf1Spec = MGF1ParameterSpec.SHA384;
break;
case "SHA-512":
mgf1Spec = MGF1ParameterSpec.SHA512;
break;
default:
throw new IllegalArgumentException("Unsupported algorithm: " + transformation);
}
parameterSpec_ = new OAEPParameterSpec(hash, "MGF1", mgf1Spec, PSource.PSpecified.DEFAULT);
} else {
// PKCS #1 v1.x
transformation_ = transformation;
parameterSpec_ = null;
}
} else {
LOGGER.warning(transformation + " is not officially supported by the JceMasterKey");
// Unsupported transformation, just use exactly what we are given
transformation_ = transformation;
parameterSpec_ = null;
}
}
/**
* Test for <code>getDigestAlgorithm()</code> method
* Assertion: returns message digest algorithm name
*/
public final void testGetDigestAlgorithm() {
PSSParameterSpec pssps = new PSSParameterSpec("SHA-1", "MGF1",
MGF1ParameterSpec.SHA1, 20, 1);
assertEquals("SHA-1", pssps.getDigestAlgorithm());
}
/**
* Test for <code>getMGFAlgorithm()</code> method
* Assertion: returns mask generation function algorithm name
*/
public final void testGetMGFAlgorithm() {
PSSParameterSpec pssps = new PSSParameterSpec("SHA-1", "MGF1",
MGF1ParameterSpec.SHA1, 20, 1);
assertEquals("MGF1", pssps.getMGFAlgorithm());
}
/**
* Test #1 for <code>getMGFParameters()</code> method
* Assertion: returns mask generation function parameters
*/
public final void testGetMGFParameters01() {
PSSParameterSpec pssps = new PSSParameterSpec("SHA-1", "MGF1",
MGF1ParameterSpec.SHA1, 20, 1);
assertTrue(MGF1ParameterSpec.SHA1.equals(pssps.getMGFParameters()));
}
/**
* Test for <code>getTrailerField()</code> method<br>
* Assertion: returns trailer field value
*/
public final void testGetTrailerField() {
PSSParameterSpec pssps = new PSSParameterSpec("SHA-1", "MGF1",
MGF1ParameterSpec.SHA1, 20, 1);
assertEquals(1, pssps.getTrailerField());
}
/**
* Test #2 for
* <code>
* PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
* </code> ctor<br>
* Assertion:
* throws <code>NullPointerException</code>
* if <code>mdName</code> is null
*/
public final void testPSSParameterSpec0202() {
try {
new PSSParameterSpec(null, "MGF1", MGF1ParameterSpec.SHA1, 20, 1);
fail("Expected NPE not thrown");
} catch (NullPointerException e) {
}
}
/**
* Test #3 for
* <code>
* PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
* </code> ctor<br>
* Assertion:
* throws <code>NullPointerException</code>
* if <code>mgfName</code> is null
*/
public final void testPSSParameterSpec0203() {
try {
new PSSParameterSpec("SHA-1", null, MGF1ParameterSpec.SHA1, 20, 1);
fail("Expected NPE not thrown");
} catch (NullPointerException e) {
}
}
/**
* Test #4 for
* <code>
* PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
* </code> ctor<br>
* Assertion:
* throws <code>IllegalArgumentException<code>
* if <code>saltLen<code> less than 0
*/
public final void testPSSParameterSpec0204() {
try {
new PSSParameterSpec("SHA-1", "MGF1",
MGF1ParameterSpec.SHA1, -20, 1);
fail("Expected IAE not thrown");
} catch (IllegalArgumentException e) {
}
}
/**
* Test #5 for
* <code>
* PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
* </code> ctor<br>
* Assertion:
* throws <code>IllegalArgumentException</code>
* if <code>trailerField</code> less than 0
*/
public final void testPSSParameterSpec0205() {
try {
new PSSParameterSpec("SHA-1", "MGF1",
MGF1ParameterSpec.SHA1, 20, -1);
fail("Expected IAE not thrown");
} catch (IllegalArgumentException e) {
}
}
/**
* Test #1 for
* <code>
* PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
* </code> ctor<br>
* Assertion: constructs using valid parameters
* <code>PSSParameterSpec<code> object
*/
public final void testPSSParameterSpec0201() {
AlgorithmParameterSpec aps = new PSSParameterSpec("SHA-1", "MGF1",
MGF1ParameterSpec.SHA1, 20, 1);
assertTrue(aps instanceof PSSParameterSpec);
}