use of org.eclipse.scout.rt.platform.util.Assertions.AssertionException in project scout.rt by eclipse.
the class SunSecurityProvider method createMac.
@Override
public byte[] createMac(byte[] password, InputStream data) {
Assertions.assertGreater(Assertions.assertNotNull(password, "no password provided").length, 0, "empty password not allowed");
if (data == null) {
throw new AssertionException("no data provided");
}
try {
String algorithm = getMacAlgorithm();
SecretKeySpec key = new SecretKeySpec(password, 0, password.length, algorithm);
Mac mac = Mac.getInstance(algorithm, getMacAlgorithmProvider());
mac.init(key);
int n;
byte[] buf = new byte[BUF_SIZE];
while ((n = data.read(buf)) >= 0) {
mac.update(buf, 0, n);
}
return mac.doFinal();
} catch (NoSuchAlgorithmException | InvalidKeyException | IllegalStateException | IOException | NoSuchProviderException e) {
throw new ProcessingException("unable to create signature.", e);
}
}
use of org.eclipse.scout.rt.platform.util.Assertions.AssertionException in project scout.rt by eclipse.
the class SunSecurityProvider method verifySignature.
@Override
public boolean verifySignature(byte[] publicKey, InputStream data, byte[] signatureToVerify) {
Assertions.assertGreater(Assertions.assertNotNull(publicKey, "no public key provided").length, 0, "empty public key not allowed");
Assertions.assertGreater(Assertions.assertNotNull(signatureToVerify, "no signature provided").length, 0, "empty signature not allowed");
if (data == null) {
throw new AssertionException("no data provided");
}
try {
// create public key from bytes
KeyFactory keyFactory = KeyFactory.getInstance(getKeyPairGenerationAlgorithm(), getSignatureProvider());
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(publicKey);
PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
// verify signature
Signature sig = Signature.getInstance(getSignatureAlgorithm(), getSignatureProvider());
sig.initVerify(pubKey);
int n;
byte[] buf = new byte[BUF_SIZE];
while ((n = data.read(buf)) >= 0) {
sig.update(buf, 0, n);
}
return sig.verify(signatureToVerify);
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException | InvalidKeyException | SignatureException | IOException t) {
throw new ProcessingException("unable to verify signature", t);
}
}
use of org.eclipse.scout.rt.platform.util.Assertions.AssertionException in project scout.rt by eclipse.
the class SunSecurityProvider method doCrypt.
protected void doCrypt(InputStream input, OutputStream output, EncryptionKey key, int mode) {
Assertions.assertNotNull(key, "key must not be null.");
if (input == null) {
throw new AssertionException("input must not be null.");
}
if (output == null) {
throw new AssertionException("output must not be null.");
}
try {
Cipher cipher = Cipher.getInstance(getCipherAlgorithm() + "/" + getCipherAlgorithmMode() + "/" + getCipherAlgorithmPadding(), getCipherAlgorithmProvider());
cipher.init(mode, key.get(), key.params());
try (OutputStream out = new CipherOutputStream(output, cipher)) {
int n;
byte[] buf = new byte[BUF_SIZE];
while ((n = input.read(buf)) >= 0) {
out.write(buf, 0, n);
}
}
} catch (NoSuchAlgorithmException e) {
throw new ProcessingException("Unable to crypt data. Algorithm could not be found. Make sure to use JRE 1.8 or newer.", e);
} catch (NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchProviderException | IOException e) {
throw new ProcessingException("Unable to crypt data.", e);
}
}
use of org.eclipse.scout.rt.platform.util.Assertions.AssertionException in project scout.rt by eclipse.
the class SunSecurityProvider method createSignature.
@Override
public byte[] createSignature(byte[] privateKey, InputStream data) {
Assertions.assertGreater(Assertions.assertNotNull(privateKey, "no private key provided").length, 0, "empty private key not allowed");
if (data == null) {
throw new AssertionException("no data provided");
}
try {
// create private key from bytes
KeyFactory keyFactory = KeyFactory.getInstance(getKeyPairGenerationAlgorithm(), getSignatureProvider());
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKey);
PrivateKey priv = keyFactory.generatePrivate(privateKeySpec);
// create signature
Signature sig = Signature.getInstance(getSignatureAlgorithm(), getSignatureProvider());
sig.initSign(priv, createSecureRandom());
int n;
byte[] buf = new byte[BUF_SIZE];
while ((n = data.read(buf)) >= 0) {
sig.update(buf, 0, n);
}
return sig.sign();
} catch (NoSuchProviderException | NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | SignatureException | IOException e) {
throw new ProcessingException("unable to create signature.", e);
}
}
use of org.eclipse.scout.rt.platform.util.Assertions.AssertionException in project scout.rt by eclipse.
the class MutualExclusionTest method testAcquisition2.
/**
* Tests blocking mutex acquisition.
*/
@Test(timeout = 1000)
public void testAcquisition2() {
ExecutionSemaphore mutex = (ExecutionSemaphore) m_task1.getExecutionSemaphore();
assertEquals(0, mutex.getCompetitorCount());
// Make task1 to acquire the mutex.
final AtomicReference<Thread> thread = new AtomicReference<>();
assertTrue(mutex.compete(m_task1, QueuePosition.HEAD, new IPermitAcquiredCallback() {
@Override
public void onPermitAcquired() {
thread.set(Thread.currentThread());
}
}));
assertSame(Thread.currentThread(), thread.get());
assertTrue(mutex.isPermitOwner(m_task1));
assertEquals(1, mutex.getCompetitorCount());
// Wrong mutex release.
try {
mutex.release(m_task2);
fail();
} catch (AssertionException e) {
assertTrue(mutex.isPermitOwner(m_task1));
assertEquals(1, mutex.getCompetitorCount());
}
// Task1 releases the mutex.
mutex.release(m_task1);
assertFalse(mutex.isPermitOwner(m_task1));
assertEquals(0, mutex.getCompetitorCount());
}
Aggregations