-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The JVM core updated to include more common components.
- Loading branch information
Showing
14 changed files
with
580 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.gossiperl.client; | ||
|
||
public class Util { | ||
public static long getTimestamp() { | ||
return (long)System.currentTimeMillis()/1000; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.gossiperl.client.encryption; | ||
|
||
import org.apache.log4j.Logger; | ||
|
||
import javax.crypto.*; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.io.*; | ||
import java.security.*; | ||
|
||
public class Aes256 { | ||
|
||
private SecretKeySpec key; | ||
private static Logger LOG = Logger.getLogger(Aes256.class); | ||
|
||
public Aes256(String key) throws NoSuchAlgorithmException, UnsupportedEncodingException { | ||
MessageDigest md = MessageDigest.getInstance("SHA-256"); | ||
byte[] digestBytes = md.digest(key.getBytes("utf-8")); | ||
this.key = new SecretKeySpec(digestBytes, "AES"); | ||
} | ||
|
||
public byte[] encrypt(byte[] data) throws NoSuchAlgorithmException, | ||
NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, | ||
IllegalBlockSizeException, BadPaddingException, NoSuchProviderException { | ||
byte[] ivBytes = generateIv(); | ||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
cipher.init(Cipher.ENCRYPT_MODE, this.key, new IvParameterSpec(ivBytes)); | ||
try { | ||
byte[] encrypted = cipher.doFinal(data); | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
outputStream.write(ivBytes); | ||
outputStream.write(encrypted); | ||
return outputStream.toByteArray(); | ||
} catch (Exception ex) { | ||
return null; | ||
} | ||
} | ||
|
||
public byte[] decrypt(byte[] data) throws NoSuchAlgorithmException, | ||
NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, | ||
IllegalBlockSizeException, BadPaddingException, NoSuchProviderException { | ||
byte[] ivBytes = new byte[16]; | ||
byte[] message = new byte[ data.length - 16 ]; | ||
System.arraycopy(data, 0, ivBytes, 0, ivBytes.length); | ||
System.arraycopy(data, ivBytes.length, message, 0, message.length); | ||
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); | ||
cipher.init(Cipher.DECRYPT_MODE, this.key, new IvParameterSpec(ivBytes)); | ||
return cipher.doFinal(message); | ||
} | ||
|
||
private byte[] generateIv() { | ||
SecureRandom random = new SecureRandom(); | ||
byte[] ivBytes = new byte[16]; | ||
random.nextBytes(ivBytes); | ||
return ivBytes; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/gossiperl/client/exceptions/GossiperlClientException.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.gossiperl.client.exceptions; | ||
|
||
public class GossiperlClientException extends Exception { | ||
public GossiperlClientException() {} | ||
public GossiperlClientException(String message) { super(message); } | ||
public GossiperlClientException(Throwable cause) { super(cause); } | ||
public GossiperlClientException(String message, Throwable cause) { super(message, cause); } | ||
} |
5 changes: 5 additions & 0 deletions
5
...n/java/com/gossiperl/client/exceptions/GossiperlUnsupportedSerializableTypeException.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.gossiperl.client.exceptions; | ||
|
||
public class GossiperlUnsupportedSerializableTypeException extends Exception { | ||
public GossiperlUnsupportedSerializableTypeException(String message) { super(message); } | ||
} |
106 changes: 106 additions & 0 deletions
106
src/main/java/com/gossiperl/client/serialization/CustomDigestField.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.gossiperl.client.serialization; | ||
|
||
import com.gossiperl.client.exceptions.GossiperlClientException; | ||
import org.apache.thrift.protocol.TType; | ||
|
||
public class CustomDigestField { | ||
|
||
private String fieldName; | ||
private Object value; | ||
private byte type; | ||
private short fieldOrder; | ||
|
||
public CustomDigestField(String fieldName, String value, int fieldOrder) | ||
throws GossiperlClientException { | ||
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) { | ||
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + "."); | ||
} | ||
this.fieldName = fieldName; | ||
this.fieldOrder = (short)fieldOrder; | ||
this.value = value; | ||
this.type = TType.STRING; | ||
} | ||
|
||
public CustomDigestField(String fieldName, boolean value, int fieldOrder) | ||
throws GossiperlClientException { | ||
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) { | ||
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + "."); | ||
} | ||
this.fieldName = fieldName; | ||
this.fieldOrder = (short)fieldOrder; | ||
this.value = new Boolean(value); | ||
this.type = TType.BOOL; | ||
} | ||
|
||
public CustomDigestField(String fieldName, byte value, int fieldOrder) | ||
throws GossiperlClientException { | ||
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) { | ||
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + "."); | ||
} | ||
this.fieldName = fieldName; | ||
this.fieldOrder = (short)fieldOrder; | ||
this.value = new Byte(value); | ||
this.type = TType.BYTE; | ||
} | ||
|
||
public CustomDigestField(String fieldName, double value, int fieldOrder) | ||
throws GossiperlClientException { | ||
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) { | ||
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + "."); | ||
} | ||
this.fieldName = fieldName; | ||
this.fieldOrder = (short)fieldOrder; | ||
this.value = new Double(value); | ||
this.type = TType.DOUBLE; | ||
} | ||
|
||
public CustomDigestField(String fieldName, short value, int fieldOrder) | ||
throws GossiperlClientException { | ||
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) { | ||
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + "."); | ||
} | ||
this.fieldName = fieldName; | ||
this.fieldOrder = (short)fieldOrder; | ||
this.value = new Short(value); | ||
this.type = TType.I16; | ||
} | ||
|
||
public CustomDigestField(String fieldName, int value, int fieldOrder) | ||
throws GossiperlClientException { | ||
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) { | ||
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + "."); | ||
} | ||
this.fieldName = fieldName; | ||
this.fieldOrder = (short)fieldOrder; | ||
this.value = new Integer(value); | ||
this.type = TType.I32; | ||
} | ||
|
||
public CustomDigestField(String fieldName, long value, int fieldOrder) | ||
throws GossiperlClientException { | ||
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) { | ||
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + "."); | ||
} | ||
this.fieldName = fieldName; | ||
this.fieldOrder = (short)fieldOrder; | ||
this.value = new Long(value); | ||
this.type = TType.I64; | ||
} | ||
|
||
public String getFieldName() { | ||
return fieldName; | ||
} | ||
|
||
public Object getValue() { | ||
return value; | ||
} | ||
|
||
public byte getType() { | ||
return type; | ||
} | ||
|
||
public short getFieldOrder() { | ||
return fieldOrder; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/gossiperl/client/serialization/DeserializeKillPill.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.gossiperl.client.serialization; | ||
|
||
/** | ||
* Created by rad on 18/12/14. | ||
*/ | ||
public class DeserializeKillPill extends DeserializeResult { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/gossiperl/client/serialization/DeserializeResult.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.gossiperl.client.serialization; | ||
|
||
public abstract class DeserializeResult { | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/gossiperl/client/serialization/DeserializeResultCustomOK.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.gossiperl.client.serialization; | ||
|
||
import java.util.Map; | ||
|
||
public class DeserializeResultCustomOK extends DeserializeResult { | ||
private String digestType; | ||
private Map<String, Object> resultData; | ||
public DeserializeResultCustomOK(String digestType, Map<String, Object> resultData) { | ||
this.digestType = digestType; | ||
this.resultData = resultData; | ||
} | ||
public String getDigestType() { | ||
return this.digestType; | ||
} | ||
public Map<String, Object> getResultData() { | ||
return this.resultData; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/gossiperl/client/serialization/DeserializeResultError.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.gossiperl.client.serialization; | ||
|
||
import com.gossiperl.client.exceptions.GossiperlClientException; | ||
|
||
public class DeserializeResultError extends DeserializeResult { | ||
private GossiperlClientException cause; | ||
public DeserializeResultError(GossiperlClientException ex) { | ||
this.cause = ex; | ||
} | ||
public GossiperlClientException getCause() { | ||
return this.cause; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/gossiperl/client/serialization/DeserializeResultForward.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.gossiperl.client.serialization; | ||
|
||
import com.gossiperl.client.thrift.DigestEnvelope; | ||
import org.apache.thrift.TBase; | ||
|
||
public class DeserializeResultForward extends DeserializeResult { | ||
|
||
private String digestType; | ||
private byte[] binaryEnvelope; | ||
private String envelopeId; | ||
|
||
public DeserializeResultForward(String digestType, byte[] binaryEnvelope, String envelopeId) { | ||
this.digestType = digestType; | ||
this.binaryEnvelope = binaryEnvelope; | ||
this.envelopeId = envelopeId; | ||
} | ||
|
||
public String getDigestType() { | ||
return digestType; | ||
} | ||
|
||
public byte[] getBinaryEnvelope() { | ||
return binaryEnvelope; | ||
} | ||
|
||
public String getEnvelopeId() { | ||
return envelopeId; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/gossiperl/client/serialization/DeserializeResultOK.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.gossiperl.client.serialization; | ||
|
||
import org.apache.thrift.TBase; | ||
|
||
public class DeserializeResultOK extends DeserializeResult { | ||
private String digestType; | ||
private TBase digest; | ||
public DeserializeResultOK(String digestType, TBase digest) { | ||
this.digestType = digestType; | ||
this.digest = digest; | ||
} | ||
public String getDigestType() { | ||
return this.digestType; | ||
} | ||
public TBase getDigest() { | ||
return this.digest; | ||
} | ||
} |
Oops, something went wrong.