/** * Sign the source document represented by the input stream. The document can be way too large to * load the whole into memory, so implementations should calculate the signature by reading * portions of it from the stream. * * @param input document to sign * @return digital signature of the document * @throws IOException if the document can't be read, is empty and like. */ public byte[] sign(InputStream input, HashType hashType) throws IOException { return sign(input, hashType, null); }
}}; AbstractPrivateKey rsaPrivateKey = new RSAOAEPPrivateKey(); rsaPrivateKey.updateFromHash(mapRSA); AbstractPublicKey rsaPublicKey = rsaPrivateKey.getPublicKey(); assertTrue(rsaPrivateKey.isInitialized()); assertEquals(rsaPrivateKey.getBitStrength(), 1024); ((RSAOAEPPrivateKey) rsaPrivateKey).resetDecryptor(); assertArrayEquals(rsaPrivateKey.decrypt(oaepSpec.C), oaepSpec.M);
/** * Test {@link RSAOAEPPrivateKey#generate} and other basic methods: * {@link RSAOAEPPrivateKey#isInitialized}, {@link RSAOAEPPrivateKey#getBitStrength}. */ @Test public void generateAndBasicMethods() throws Exception { assertTrue(randomPrivateKey4096.isInitialized()); assertEquals(randomPrivateKey4096.getBitStrength(), 4096); assertTrue(randomPrivateKey4096.canDecrypt()); }
Map mapRSA = rsaPrivateKey.toHash(); assertArrayEquals((byte[]) mapRSA.get("e"), oaepSpec.e); assertArrayEquals((byte[]) mapRSA.get("p"), oaepSpec.p); AbstractPublicKey randomPublicKey4096 = randomPrivateKey4096.getPublicKey(); Map mapPrivate4096 = randomPrivateKey4096.toHash(), mapPublic4096 = randomPublicKey4096.toHash(); RSAPrivateCrtKeyParameters privateKeyParameters4096 = ((RSAOAEPPrivateKey) randomPrivateKey4096).state.keyParameters; RSAKeyParameters publicKeyParameters4096 = ((RSAOAEPPublicKey) (randomPrivateKey4096.getPublicKey())).state.keyParameters;
AbstractPublicKey rsaPublicKey = rsaPrivateKey.getPublicKey(); ((RSAOAEPPrivateKey) rsaPrivateKey).resetDecryptor(); assertArrayEquals(rsaPrivateKey.decrypt(oaepSpec.C), oaepSpec.M); ((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor(); assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C); AbstractPublicKey randomPublicKey = randomPrivateKey.getPublicKey(); byte[] encrypted = randomPublicKey.encrypt(message), decrypted = randomPrivateKey.decrypt(encrypted); assertArrayEquals(decrypted, message);
/** * Test {@link RSAOAEPPrivateKey#getPublicKey}. */ @Test public void getPublicKey() throws Exception { AbstractPublicKey randomPublicKey4096 = randomPrivateKey4096.getPublicKey(); assertTrue(randomPublicKey4096 instanceof RSAOAEPPublicKey); AbstractPrivateKey rsaPrivateKey = oaepSpec.getPrivateKey(); AbstractPublicKey rsaPublicKey = rsaPrivateKey.getPublicKey(); ((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor(); assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C); }
/** * Test {@link RSAOAEPPrivateKey#updateFromHash} on success scenarios, * on some sample vector. */ @Test public void updateFromHashGoodSample() throws Exception { Map map = new HashMap<String, Object>() {{ put("e", Hex.decode("010001")); put("p", Hex.decode("d4f56e648a7fe26ab052144abfd9a9cdb3ebf220ac05c3e3eb13e42c245ab0578e185a8782bf9b4c1fcc1f577a01cf9c86afef7b4fbb22ecf4fee4409f94c40da7527e0a084ae274494826a7352de4611d36b21bd3d43b4684d997dffff76765b2033a2dd949ccc5521bf1b2889c75a4ce5ce7eb09269dfeaa1f859449c4f359fcbbfc6a00593c1948665c494e7c7edd185c541a7709de5e0528345d0fc31bd214b4d36a7f96cea8fc003730fbeff97bb14267c14dad794d4f949a65fa10fcbc27d1df170128c42d294161ccdaec5e4886da42920d4f6d89dad7a038625111cd75f5d0268009d4fed07adb08da146729e3bf8e882646ad97df0a1e9391656951")); put("q", Hex.decode("c7524e21b6d44fe1e7467168b5888047216584821d4b12370f5ca42eefdf729c9f328fe4eaad06ae52aff40bf099d5e8383b34a6a5ea6d52fadf1178546b7afa48eee25801015fe174294a59c0f3fe24f8c3dcdc1572c96b602235e4c26e0fa85c2e9852f529574afb7ccc53ba47c6705de1170bd5b41cd8ca80efd1c324e1a5091a0a5a895d00b857185fd73fd284da14503b6b48a5ee165ce981732ae163d21ae4426204bb8a7e23c6793d4e9c9501a57abbf5457f745ff62e78d8bb250fe41bec2b3c6f77e64876a1d63d0b19ead7e393c7507358b5dbc4ce2c21554c00aa8e8d38c78e72ce0ed306e9c3ae0cd4cc1e92842caf7b70e883daf43b0cf25267")); put("mgf1Hash", "SHA-512"); }}; AbstractPrivateKey privateKey = new RSAOAEPPrivateKey(); privateKey.updateFromHash(map); AbstractPublicKey publicKey = privateKey.getPublicKey(); assertTrue(privateKey.isInitialized()); assertEquals(privateKey.getBitStrength(), 4096); // Also test that the public key counterpart is also deserialized properly // and is capable of encrypting the data (after deserialization) as needed. assertTrue(publicKey.isInitialized()); assertEquals(publicKey.getBitStrength(), 4096); RSAOAEPPublicKey.State publicKeyState = ((RSAOAEPPublicKey)publicKey).state; assertEquals(publicKeyState.oaepHashType, HashType.SHA1); // Default assertEquals(publicKeyState.mgf1HashType, HashType.SHA512); RSAOAEPPrivateKey.State privateKeyState = ((RSAOAEPPrivateKey)privateKey).state; assertEquals(privateKeyState.oaepHashType, HashType.SHA1); // Default assertEquals(privateKeyState.mgf1HashType, HashType.SHA512); }
public void toHashDefaultData() throws Exception { AbstractPublicKey randomPublicKey4096SHA256 = randomPrivateKey4096SHADefault.getPublicKey(); Map mapPrivate4096SHA256 = randomPrivateKey4096SHADefault.toHash(), mapPublic4096SHA256 = randomPublicKey4096SHA256.toHash(); RSAPrivateCrtKeyParameters privateKeyParameters4096SHA256 = ((RSAOAEPPrivateKey) randomPrivateKey4096SHADefault).state.keyParameters; RSAKeyParameters publicKeyParameters4096 = ((RSAOAEPPublicKey) (randomPrivateKey4096SHADefault.getPublicKey())).state.keyParameters;
privateKey.updateFromHash(map); AbstractPublicKey publicKey = privateKey.getPublicKey(); assertTrue(privateKey.isInitialized()); assertEquals(privateKey.getBitStrength(), 4096);
/** * Digitally sign a string encoded in UTF-8. * * @param data to sign * @return digital signature */ public byte[] sign(String data, HashType hashType) { return sign(data.getBytes(), hashType); } }
/** * Digitally sign data in array. * * @param data to sign * @return digital signature */ public byte[] sign(byte[] data, HashType hashType) { try { return sign(new ByteArrayInputStream((data)), hashType); } catch (IOException e) { throw new RuntimeException("Failed to sign", e); } }
/** * Digitally sign a string encoded in UTF-8. * * @param data to sign * @param salt salt to use; may be null, then it is generated automatically with the maximum possible size. * @return digital signature */ public byte[] sign(String data, HashType hashType, @Nullable byte[] salt) { return sign(data.getBytes(), hashType, salt); }
/** * Digitally sign data in array. * * @param data to sign * @param salt salt to use; may be null, then it is generated automatically with the maximum possible size. * @return digital signature */ public byte[] sign(byte[] data, HashType hashType, @Nullable byte[] salt) { try { return sign(new ByteArrayInputStream((data)), hashType, salt); } catch (IOException e) { throw new RuntimeException("Failed to sign", e); } }
/** * Test {@link RSAOAEPPrivateKey#sign}. */ @Test public void sign() throws Exception { // Test on RSA vectors. AbstractPrivateKey rsaPrivateKey = pssSpec.getPrivateKey(); assertArrayEquals(rsaPrivateKey.sign(pssSpec.M, HashType.SHA1, RSASSAPSSTestVectors.salt), pssSpec.S); }
/** * Test {@link RSAOAEPPublicKey#checkSignature}. */ @Test public void checkSignature() throws Exception { // Test sample RSA vectors. AbstractPublicKey rsaPublicKey = pssSpec.getPublicKey(); AbstractPrivateKey rsaPrivateKey = pssSpec.getPrivateKey(); assertArrayEquals( rsaPrivateKey.sign(pssSpec.M, HashType.SHA1, RSASSAPSSTestVectors.salt), pssSpec.S); assertTrue(rsaPublicKey.checkSignature( pssSpec.M, rsaPrivateKey.sign(pssSpec.M, HashType.SHA1, RSASSAPSSTestVectors.salt), HashType.SHA1, RSASSAPSSTestVectors.salt.length)); }