Permalink
Browse files

Merge pull request #87 from WindowsAzure/dev

Dev
  • Loading branch information...
2 parents 4ed9259 + 9a0d6fb commit 811ff73250001b43e6a02354116002c7a3741d5a Albert Cheng committed Jan 9, 2013
@@ -104,12 +104,6 @@
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
- <dependency>
- <groupId>org.bouncycastle</groupId>
- <artifactId>bcprov-jdk16</artifactId>
- <version>1.46</version>
- <scope>test</scope>
- </dependency>
</dependencies>
<build>
@@ -135,7 +135,6 @@ public LinkInfo getLocatorsLink() {
* @return the link
*/
public LinkInfo getParentAssetsLink() {
- // TODO: NEEDS TESTS once we figure out how to create assets with parents
return getRelationLink("ParentAssets");
}
}
@@ -21,7 +21,6 @@
import com.microsoft.windowsazure.services.media.implementation.atom.EntryType;
import com.microsoft.windowsazure.services.media.implementation.content.JobType;
-// TODO: Auto-generated Javadoc
/**
* The Class JobInfo.
*/
@@ -19,7 +19,6 @@
import com.microsoft.windowsazure.services.media.implementation.atom.EntryType;
import com.microsoft.windowsazure.services.media.implementation.content.MediaProcessorType;
-// TODO: Auto-generated Javadoc
/**
* Data about a Media Processor entity.
*
@@ -82,8 +82,6 @@ public void canCreateAccessPolicyWithReadPermissions() throws Exception {
verifyPolicyProperties("policy", testName, duration, AccessPolicyPermission.READ, policy);
}
- // TODO: Null name or duration?
-
@Test
public void canGetSinglePolicyById() throws Exception {
String expectedName = testPolicyPrefix + "GetOne";
@@ -17,23 +17,38 @@
import static org.junit.Assert.*;
+import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
+import java.util.EnumSet;
import java.util.List;
import java.util.UUID;
import org.junit.Test;
import com.microsoft.windowsazure.services.core.ServiceException;
+import com.microsoft.windowsazure.services.media.models.AccessPolicy;
+import com.microsoft.windowsazure.services.media.models.AccessPolicyInfo;
+import com.microsoft.windowsazure.services.media.models.AccessPolicyPermission;
import com.microsoft.windowsazure.services.media.models.Asset;
+import com.microsoft.windowsazure.services.media.models.AssetFile;
import com.microsoft.windowsazure.services.media.models.AssetInfo;
import com.microsoft.windowsazure.services.media.models.AssetOption;
import com.microsoft.windowsazure.services.media.models.AssetState;
import com.microsoft.windowsazure.services.media.models.ContentKey;
import com.microsoft.windowsazure.services.media.models.ContentKeyInfo;
import com.microsoft.windowsazure.services.media.models.ContentKeyType;
+import com.microsoft.windowsazure.services.media.models.Job;
+import com.microsoft.windowsazure.services.media.models.JobInfo;
+import com.microsoft.windowsazure.services.media.models.LinkInfo;
+import com.microsoft.windowsazure.services.media.models.ListResult;
+import com.microsoft.windowsazure.services.media.models.Locator;
+import com.microsoft.windowsazure.services.media.models.LocatorInfo;
+import com.microsoft.windowsazure.services.media.models.LocatorType;
+import com.microsoft.windowsazure.services.media.models.Task;
+import com.microsoft.windowsazure.services.media.models.Task.CreateBatchOperation;
public class AssetIntegrationTest extends IntegrationTestBase {
@@ -301,4 +316,43 @@ public void linkAssetContentKeyInvalidIdFailed() throws ServiceException, URISyn
// Assert
}
+
+ @Test
+ public void canGetParentBackFromAsset() throws ServiceException, InterruptedException {
+ // Arrange
+ String originalAssetName = testAssetPrefix + "canGetParentBackFromAsset";
+ AssetInfo originalAsset = service.create(Asset.create().setName(originalAssetName));
+
+ int durationInMinutes = 10;
+ AccessPolicyInfo accessPolicyInfo = service.create(AccessPolicy.create(testPolicyPrefix
+ + "uploadAesPortectedAssetSuccess", durationInMinutes, EnumSet.of(AccessPolicyPermission.WRITE)));
+
+ LocatorInfo locatorInfo = service.create(Locator.create(accessPolicyInfo.getId(), originalAsset.getId(),
+ LocatorType.SAS));
+ WritableBlobContainerContract blobWriter = service.createBlobWriter(locatorInfo);
+
+ InputStream mpeg4H264InputStream = getClass().getResourceAsStream("/media/MPEG4-H264.mp4");
+ blobWriter.createBlockBlob("MPEG4-H264.mp4", mpeg4H264InputStream);
+ service.action(AssetFile.createFileInfos(originalAsset.getId()));
+
+ String jobName = testJobPrefix + "createJobSuccess";
+ CreateBatchOperation taskCreator = Task
+ .create("nb:mpid:UUID:2f381738-c504-4e4a-a38e-d199e207fcd5",
+ "<taskBody>" + "<inputAsset>JobInputAsset(0)</inputAsset>"
+ + "<outputAsset>JobOutputAsset(0)</outputAsset>" + "</taskBody>")
+ .setConfiguration("H.264 256k DSL CBR").setName("My encoding Task");
+ JobInfo jobInfo = service.create(Job.create().setName(jobName).addInputMediaAsset(originalAsset.getId())
+ .addTaskCreator(taskCreator));
+
+ // Act
+ ListResult<AssetInfo> outputAssets = service.list(Asset.list(jobInfo.getOutputAssetsLink()));
+ assertEquals(1, outputAssets.size());
+ AssetInfo childAsset = outputAssets.get(0);
+
+ LinkInfo parentAssetLink = childAsset.getParentAssetsLink();
+ AssetInfo parentAsset = service.get(Asset.get(parentAssetLink));
+
+ // Assert
+ assertEquals(originalAsset.getId(), parentAsset.getId());
+ }
}
@@ -16,100 +16,50 @@
package com.microsoft.windowsazure.services.media;
import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
import java.io.InputStream;
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidKeyException;
import java.security.Key;
-import java.security.NoSuchAlgorithmException;
-import java.security.NoSuchProviderException;
import java.security.SecureRandom;
-import java.security.cert.CertificateException;
+import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
-import java.security.cert.X509Certificate;
-import java.util.Random;
-import java.util.UUID;
-import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
-import javax.crypto.IllegalBlockSizeException;
-import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.microsoft.windowsazure.services.core.storage.utils.Base64;
-public class EncryptionHelper {
-
- public static byte[] createRandomVector(int numberOfBits) {
- int numberOfBytes = numberOfBits / 8;
- byte[] randomVector = new byte[numberOfBytes];
- Random random = new Random();
- random.nextBytes(randomVector);
- return randomVector;
- }
-
- public static byte[] EncryptSymmetricKey(String protectionKey, byte[] inputData) throws Exception {
-
- X509Certificate x509Certificate = createX509CertificateFromString(protectionKey);
- return EncryptSymmetricKey(x509Certificate.getPublicKey(), inputData);
- }
-
- private static X509Certificate createX509CertificateFromString(String protectionKey) throws CertificateException {
+class EncryptionHelper {
+ public static byte[] encryptSymmetricKey(String protectionKey, byte[] inputData) throws Exception {
+ Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
- ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(Base64.decode(protectionKey));
- X509Certificate x509cert = (X509Certificate) certificateFactory.generateCertificate(byteArrayInputStream);
- return x509cert;
-
- }
-
- public static byte[] EncryptSymmetricKey(Key publicKey, byte[] inputData) throws NoSuchAlgorithmException,
- NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
- BadPaddingException {
- Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC");
+ byte[] protectionKeyBytes = Base64.decode(protectionKey);
+ ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(protectionKeyBytes);
+ Certificate certificate = certificateFactory.generateCertificate(byteArrayInputStream);
+ Key publicKey = certificate.getPublicKey();
SecureRandom secureRandom = new SecureRandom();
cipher.init(Cipher.ENCRYPT_MODE, publicKey, secureRandom);
byte[] cipherText = cipher.doFinal(inputData);
return cipherText;
}
- public static String calculateChecksum(UUID uuid, byte[] aesKey) throws NoSuchAlgorithmException,
- NoSuchProviderException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException,
- InvalidKeyException {
+ public static String calculateContentKeyChecksum(String uuid, byte[] aesKey) throws Exception {
+ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKeySpec = new SecretKeySpec(aesKey, "AES");
- Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING", "BC");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
- byte[] encryptionResult = cipher.doFinal(uuid.toString().getBytes());
+ byte[] encryptionResult = cipher.doFinal(uuid.getBytes("UTF8"));
byte[] checksumByteArray = new byte[8];
- for (int i = 0; i < 8; i++) {
- checksumByteArray[i] = encryptionResult[i];
- }
+ System.arraycopy(encryptionResult, 0, checksumByteArray, 0, 8);
String checksum = Base64.encode(checksumByteArray);
return checksum;
}
- public static byte[] EncryptFile(InputStream inputStream, byte[] aesKey, byte[] initializationVector)
- throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException,
- InvalidAlgorithmParameterException, IOException {
- // preparation
- SecretKeySpec key = new SecretKeySpec(aesKey, "AES");
- IvParameterSpec ivParameterSpec = new IvParameterSpec(initializationVector);
- Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
-
- // encryption
- cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
+ public static InputStream encryptFile(InputStream inputStream, byte[] key, byte[] iv) throws Exception {
+ Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
+ SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
+ cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec);
CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
-
- int ch;
- while ((ch = cipherInputStream.read()) >= 0) {
- byteArrayOutputStream.write(ch);
- }
-
- byte[] cipherText = byteArrayOutputStream.toByteArray();
- return cipherText;
+ return cipherInputStream;
}
-
}
Oops, something went wrong.

0 comments on commit 811ff73

Please sign in to comment.