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
def calculate_combination(num_elements, limit) | |
combos = SortedSet.new | |
base_array = (1..limit).to_a | |
base_array.combination(num_elements).each do | group | | |
combos.add(group.sort) if group.inject(0, :+) == limit | |
end | |
combos | |
end | |
combos = calculate_combination(4, 40) |
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
require 'set' | |
def calculate_combos(limit) | |
combos = SortedSet.new | |
range_limit = (1..limit).to_a | |
range_limit.each do |w| | |
range_limit.each do |x| | |
range_limit.each do |y| | |
range_limit.each do |z| | |
combos.add([w, x, y, z].sort) if w + x + y + z == limit |
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
diff --git a/src/main/java/com/rhjensen/encryption/AESEncryptor.java b/src/main/java/com/rhjensen/encryption/AESEncryptor.java | |
index 66313f9..6857481 100644 | |
--- a/src/main/java/com/rhjensen/encryption/AESEncryptor.java | |
+++ b/src/main/java/com/rhjensen/encryption/AESEncryptor.java | |
@@ -1,7 +1,6 @@ | |
package com.rhjensen.encryption; | |
-import sun.misc.BASE64Decoder; | |
-import sun.misc.BASE64Encoder; | |
+import org.apache.commons.codec.binary.Base64; |
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
//Change Base64 string decoding from: | |
new BASE64Decoder().decodeBuffer(sessionKey); | |
//To: | |
new Base64().decode(sessionKey); | |
// AND | |
//Change Base64 string encoding from: | |
return new BASE64Encoder().encode(ciphertext); | |
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
package com.rhjensen.encryption; | |
import org.junit.Before; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import sun.misc.BASE64Encoder; | |
import javax.crypto.KeyGenerator; | |
import java.security.NoSuchAlgorithmException; |
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
package com.rhjensen.encryption; | |
import sun.misc.BASE64Decoder; | |
import sun.misc.BASE64Encoder; | |
import javax.crypto.*; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; |
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
public AESEncryptor(byte[] sessionKey, byte[] iv) { | |
try { | |
encryptor = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
encryptor.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), new IvParameterSpec(iv)); | |
decryptor = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
decryptor.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), encryptor.getParameters()); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} catch (NoSuchPaddingException e) { | |
e.printStackTrace(); |
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
public class AESEncryptorTest { | |
private static String SAMPLE_TEXT = "Original Plaintext"; | |
private static KeyGenerator KEY_GENERATOR = null; | |
private static byte[] SESSION_KEY = null; | |
private static byte[] VECTOR; | |
private AESEncryptor aesExample; | |
@BeforeClass | |
public static void initializeSharedState() throws NoSuchAlgorithmException { | |
KEY_GENERATOR = KeyGenerator.getInstance("AES"); |
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
@Test | |
public void encryptingTheSameStringShouldGiveTheSameCiphertext() throws Exception { | |
String cipherText1 = aesExample.encrypt(SAMPLE_TEXT); | |
String cipherText2 = new AESEncryptor().encrypt(SAMPLE_TEXT); | |
assertEquals(cipherText1, cipherText2); | |
} |
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
public class AESEncryptorTest { | |
private final AESEncryptor aesExample = new AESEncryptor(); | |
private static String SAMPLE_TEXT = "Original Plaintext"; | |
@Test | |
public void decryptingCiphertextShouldReturnOriginalPlaintext() throws Exception { | |
String cipherText = aesExample.encrypt(SAMPLE_TEXT); | |
String plainText = aesExample.decrypt(cipherText); | |
assertEquals(SAMPLE_TEXT, plainText); | |
} |
NewerOlder