Created
May 1, 2012 19:58
-
-
Save jpoetker/2570933 to your computer and use it in GitHub Desktop.
Encrypts a file and uploads it to Atmos (POC - not robust)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.medplus.etech; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.InputStream; | |
import java.security.InvalidKeyException; | |
import java.security.NoSuchAlgorithmException; | |
import javax.crypto.Cipher; | |
import javax.crypto.CipherInputStream; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.spec.SecretKeySpec; | |
import javax.xml.bind.DatatypeConverter; | |
import com.emc.esu.api.Metadata; | |
import com.emc.esu.api.MetadataList; | |
import com.emc.esu.api.rest.EsuRestApi; | |
public class EncryptedUpload { | |
private static final SecretKeySpec KEY = new SecretKeySpec( | |
DatatypeConverter.parseBase64Binary("THIS IS A SECRET"), | |
"AES"); | |
private static long encryptedLength(long unencryptedLength) { | |
return (unencryptedLength/16 + 1) * 16; | |
} | |
private static InputStream createCipherStream(File file) throws FileNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { | |
FileInputStream fis = new FileInputStream(file); | |
Cipher cipher = Cipher.getInstance("AES"); | |
cipher.init(Cipher.ENCRYPT_MODE, KEY); | |
CipherInputStream cis = new CipherInputStream(fis, cipher); | |
return cis; | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
try { | |
File file = new File(args[0]); | |
if (file.exists()) { | |
EsuRestApi esu = new EsuRestApi("api.atmosonline.com", | |
80, | |
"uid", | |
"sharedsecret"); | |
MetadataList metadata = new MetadataList(); | |
metadata.addMetadata(new Metadata("File-Name", args[0], true)); | |
metadata.addMetadata(new Metadata("Encryption-Algorithm", "AES", true)); | |
esu.createObjectFromStream(null, | |
metadata, createCipherStream(file), encryptedLength(file.length()), null); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment