Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed the old AES tool, started writing a new one from scratch.
- Loading branch information
1 parent
462152c
commit 0d3420a
Showing
7 changed files
with
174 additions
and
143 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
rapidoid-commons/src/main/java/org/rapidoid/crypto/AES.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,41 @@ | ||
package org.rapidoid.crypto; | ||
|
||
/* | ||
* #%L | ||
* rapidoid-commons | ||
* %% | ||
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import org.rapidoid.RapidoidThing; | ||
import org.rapidoid.annotation.Authors; | ||
import org.rapidoid.annotation.Since; | ||
|
||
@Authors("Nikolche Mihajlovski") | ||
@Since("5.3.0") | ||
public class AES extends RapidoidThing { | ||
|
||
private static final AESCypherTool INSTANCE = new AESCypherTool(); | ||
|
||
public static byte[] encrypt(byte[] data, byte[] secret) throws Exception { | ||
return INSTANCE.encrypt(data, secret); | ||
} | ||
|
||
public static byte[] decrypt(byte[] data, byte[] secret) throws Exception { | ||
return INSTANCE.decrypt(data, secret); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
rapidoid-commons/src/main/java/org/rapidoid/crypto/AESCypherTool.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,39 @@ | ||
package org.rapidoid.crypto; | ||
|
||
/* | ||
* #%L | ||
* rapidoid-commons | ||
* %% | ||
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import org.rapidoid.RapidoidThing; | ||
import org.rapidoid.annotation.Authors; | ||
import org.rapidoid.annotation.Since; | ||
|
||
@Authors("Nikolche Mihajlovski") | ||
@Since("5.3.0") | ||
public class AESCypherTool extends RapidoidThing { | ||
|
||
public byte[] encrypt(byte[] data, byte[] secret) throws Exception { | ||
return null; // FIXME implement this | ||
} | ||
|
||
public byte[] decrypt(byte[] data, byte[] secret) throws Exception { | ||
return null; // FIXME implement this | ||
} | ||
|
||
} |
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
80 changes: 80 additions & 0 deletions
80
rapidoid-commons/src/main/java/org/rapidoid/crypto/CryptoUtil.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,80 @@ | ||
package org.rapidoid.crypto; | ||
|
||
/* | ||
* #%L | ||
* rapidoid-commons | ||
* %% | ||
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import keywhiz.hkdf.Hkdf; | ||
import org.rapidoid.RapidoidThing; | ||
import org.rapidoid.annotation.Authors; | ||
import org.rapidoid.annotation.Since; | ||
import org.rapidoid.u.U; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.SecretKeyFactory; | ||
import javax.crypto.spec.PBEKeySpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.spec.InvalidKeySpecException; | ||
|
||
@Authors("Nikolche Mihajlovski") | ||
@Since("5.3.0") | ||
public class CryptoUtil extends RapidoidThing { | ||
|
||
private static final Hkdf HKDF = Hkdf.usingDefaults(); | ||
|
||
private static final String HMAC_SHA_256 = "HmacSHA256"; | ||
|
||
private static final String AES_CTR_NO_PADDING = "AES/CTR/NoPadding"; | ||
|
||
public static final int AES_KEY_LENGTH = calcAESKeyLength(); | ||
|
||
private static int calcAESKeyLength() { | ||
int maxKeyLen; | ||
try { | ||
maxKeyLen = Cipher.getMaxAllowedKeyLength("AES"); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw U.rte(e); | ||
} | ||
|
||
return maxKeyLen > 256 ? 256 : 128; | ||
} | ||
|
||
public static byte[] hkdf(byte[] secret, byte[] salt, int bitLength) { | ||
SecretKeySpec key = new SecretKeySpec(secret, HMAC_SHA_256); | ||
return HKDF.expand(key, salt, bitLength / 8); | ||
} | ||
|
||
public static byte[] pbkdf2(char[] password, byte[] salt, int iterations, int length) | ||
throws NoSuchAlgorithmException, InvalidKeySpecException { | ||
|
||
PBEKeySpec keySpec = new PBEKeySpec(password, salt, iterations, length); | ||
|
||
return getPBKDFInstance().generateSecret(keySpec).getEncoded(); | ||
} | ||
|
||
private static SecretKeyFactory getPBKDFInstance() throws NoSuchAlgorithmException { | ||
try { | ||
return SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); | ||
} catch (NoSuchAlgorithmException e) { | ||
return SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); | ||
} | ||
} | ||
|
||
} |
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