Chris Lu
5 years ago
11 changed files with 308 additions and 59 deletions
-
15other/java/client/pom.xml
-
33other/java/client/src/main/java/seaweedfs/client/FilerGrpcClient.java
-
37other/java/client/src/main/java/seaweedfs/client/Gzip.java
-
55other/java/client/src/main/java/seaweedfs/client/SeaweedCipher.java
-
75other/java/client/src/main/java/seaweedfs/client/SeaweedRead.java
-
40other/java/client/src/main/java/seaweedfs/client/SeaweedWrite.java
-
42other/java/client/src/test/java/seaweedfs/client/SeaweedCipherTest.java
-
2other/java/hdfs2/dependency-reduced-pom.xml
-
2other/java/hdfs2/pom.xml
-
2other/java/hdfs3/dependency-reduced-pom.xml
-
2other/java/hdfs3/pom.xml
@ -0,0 +1,37 @@ |
|||
package seaweedfs.client; |
|||
|
|||
import java.io.ByteArrayInputStream; |
|||
import java.io.ByteArrayOutputStream; |
|||
import java.io.IOException; |
|||
import java.io.InputStream; |
|||
import java.util.zip.GZIPInputStream; |
|||
import java.util.zip.GZIPOutputStream; |
|||
|
|||
public class Gzip { |
|||
public static byte[] compress(byte[] data) throws IOException { |
|||
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); |
|||
GZIPOutputStream gzip = new GZIPOutputStream(bos); |
|||
gzip.write(data); |
|||
gzip.close(); |
|||
byte[] compressed = bos.toByteArray(); |
|||
bos.close(); |
|||
return compressed; |
|||
} |
|||
|
|||
public static byte[] decompress(byte[] compressed) throws IOException { |
|||
ByteArrayInputStream bis = new ByteArrayInputStream(compressed); |
|||
GZIPInputStream gis = new GZIPInputStream(bis); |
|||
return readAll(gis); |
|||
} |
|||
|
|||
private static byte[] readAll(InputStream input) throws IOException { |
|||
try( ByteArrayOutputStream output = new ByteArrayOutputStream()){ |
|||
byte[] buffer = new byte[4096]; |
|||
int n; |
|||
while (-1 != (n = input.read(buffer))) { |
|||
output.write(buffer, 0, n); |
|||
} |
|||
return output.toByteArray(); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,55 @@ |
|||
package seaweedfs.client; |
|||
|
|||
import javax.crypto.Cipher; |
|||
import javax.crypto.spec.GCMParameterSpec; |
|||
import javax.crypto.spec.SecretKeySpec; |
|||
import java.security.SecureRandom; |
|||
|
|||
public class SeaweedCipher { |
|||
// AES-GCM parameters |
|||
public static final int AES_KEY_SIZE = 256; // in bits |
|||
public static final int GCM_NONCE_LENGTH = 12; // in bytes |
|||
public static final int GCM_TAG_LENGTH = 16; // in bytes |
|||
|
|||
private static SecureRandom random = new SecureRandom(); |
|||
|
|||
public static byte[] genCipherKey() throws Exception { |
|||
byte[] key = new byte[AES_KEY_SIZE / 8]; |
|||
random.nextBytes(key); |
|||
return key; |
|||
} |
|||
|
|||
public static byte[] encrypt(byte[] clearTextbytes, byte[] cipherKey) throws Exception { |
|||
return encrypt(clearTextbytes, 0, clearTextbytes.length, cipherKey); |
|||
} |
|||
|
|||
public static byte[] encrypt(byte[] clearTextbytes, int offset, int length, byte[] cipherKey) throws Exception { |
|||
|
|||
final byte[] nonce = new byte[GCM_NONCE_LENGTH]; |
|||
random.nextBytes(nonce); |
|||
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, nonce); |
|||
SecretKeySpec keySpec = new SecretKeySpec(cipherKey, "AES"); |
|||
|
|||
Cipher AES_cipherInstance = Cipher.getInstance("AES/GCM/NoPadding"); |
|||
AES_cipherInstance.init(Cipher.ENCRYPT_MODE, keySpec, spec); |
|||
|
|||
byte[] encryptedText = AES_cipherInstance.doFinal(clearTextbytes, offset, length); |
|||
|
|||
byte[] iv = AES_cipherInstance.getIV(); |
|||
byte[] message = new byte[GCM_NONCE_LENGTH + clearTextbytes.length + GCM_TAG_LENGTH]; |
|||
System.arraycopy(iv, 0, message, 0, GCM_NONCE_LENGTH); |
|||
System.arraycopy(encryptedText, 0, message, GCM_NONCE_LENGTH, encryptedText.length); |
|||
|
|||
return message; |
|||
} |
|||
|
|||
public static byte[] decrypt(byte[] encryptedText, byte[] cipherKey) throws Exception { |
|||
final Cipher AES_cipherInstance = Cipher.getInstance("AES/GCM/NoPadding"); |
|||
GCMParameterSpec params = new GCMParameterSpec(GCM_TAG_LENGTH * 8, encryptedText, 0, GCM_NONCE_LENGTH); |
|||
SecretKeySpec keySpec = new SecretKeySpec(cipherKey, "AES"); |
|||
AES_cipherInstance.init(Cipher.DECRYPT_MODE, keySpec, params); |
|||
byte[] decryptedText = AES_cipherInstance.doFinal(encryptedText, GCM_NONCE_LENGTH, encryptedText.length - GCM_NONCE_LENGTH); |
|||
return decryptedText; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,42 @@ |
|||
package seaweedfs.client; |
|||
|
|||
import org.junit.Test; |
|||
|
|||
import java.util.Base64; |
|||
|
|||
import static seaweedfs.client.SeaweedCipher.decrypt; |
|||
import static seaweedfs.client.SeaweedCipher.encrypt; |
|||
|
|||
public class SeaweedCipherTest { |
|||
|
|||
@Test |
|||
public void testSameAsGoImplemnetation() throws Exception { |
|||
byte[] secretKey = "256-bit key for AES 256 GCM encr".getBytes(); |
|||
|
|||
String plainText = "Now we need to generate a 256-bit key for AES 256 GCM"; |
|||
|
|||
System.out.println("Original Text : " + plainText); |
|||
|
|||
byte[] cipherText = encrypt(plainText.getBytes(), secretKey); |
|||
System.out.println("Encrypted Text : " + Base64.getEncoder().encodeToString(cipherText)); |
|||
|
|||
byte[] decryptedText = decrypt(cipherText, secretKey); |
|||
System.out.println("DeCrypted Text : " + new String(decryptedText)); |
|||
} |
|||
|
|||
@Test |
|||
public void testEncryptDecrypt() throws Exception { |
|||
byte[] secretKey = SeaweedCipher.genCipherKey(); |
|||
|
|||
String plainText = "Now we need to generate a 256-bit key for AES 256 GCM"; |
|||
|
|||
System.out.println("Original Text : " + plainText); |
|||
|
|||
byte[] cipherText = encrypt(plainText.getBytes(), secretKey); |
|||
System.out.println("Encrypted Text : " + Base64.getEncoder().encodeToString(cipherText)); |
|||
|
|||
byte[] decryptedText = decrypt(cipherText, secretKey); |
|||
System.out.println("DeCrypted Text : " + new String(decryptedText)); |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue