This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#83] Add simple tool to encrypt text
To be used in a development process.
- Loading branch information
Marcin Zajaczkowski
committed
Nov 18, 2014
1 parent
4ca39dd
commit cf7d9b4
Showing
1 changed file
with
48 additions
and
3 deletions.
There are no files selected for viewing
51 changes: 48 additions & 3 deletions
51
...property/src/test/groovy/com/ofg/infrastructure/property/decrypt/EncryptorTestUtil.groovy
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 |
---|---|---|
@@ -1,16 +1,61 @@ | ||
package com.ofg.infrastructure.property.decrypt | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.transform.PackageScope | ||
import groovy.transform.ToString | ||
import groovy.transform.TupleConstructor | ||
import org.springframework.boot.builder.SpringApplicationBuilder | ||
import org.springframework.security.crypto.encrypt.TextEncryptor | ||
|
||
/** | ||
* Simple encryption test util. | ||
* | ||
* Note. The class shouldn't be reused as it does not do required clean up and can pollute JVM | ||
*/ | ||
@PackageScope | ||
@CompileStatic | ||
class EncryptorTestUtil { | ||
|
||
static void main(String[] args) { | ||
System.setProperty("encrypt.key", "eKey") | ||
EncryptionInputData inputData = prepareImputData(args) | ||
println "Encrypted text: ${decryptAndReturn(inputData)}" | ||
} | ||
|
||
private static String decryptAndReturn(EncryptionInputData inputData) { | ||
System.setProperty("encrypt.key", inputData.encryptKey) | ||
System.setProperty("APP_ENV", "prod") | ||
System.setProperty("countryCode", "pl") | ||
def context = new SpringApplicationBuilder(DecryptingPropertyTestApp).web(false).run() | ||
|
||
def context = new SpringApplicationBuilder(DecryptingPropertyTestApp).web(false).showBanner(false).run() | ||
def encryptor = context.getBean(TextEncryptor) | ||
println "Encrypted: ${encryptor.encrypt("value to encrypt")}" | ||
|
||
encryptor.encrypt(inputData.textToEncrypt) | ||
} | ||
|
||
private static EncryptionInputData prepareImputData(String[] args) { | ||
if (args.length == 2) { | ||
new EncryptionInputData(args[0], args[1]) | ||
} else { | ||
createUtilUsingParametersFromConsole() | ||
} | ||
} | ||
|
||
private static EncryptionInputData createUtilUsingParametersFromConsole() { | ||
def reader = new BufferedReader(new InputStreamReader(System.in)) //System.console() returns null from Idea | ||
|
||
print "Enter encryption key: " | ||
def key = reader.readLine() | ||
|
||
print "Enter text to encrypt: " | ||
def text = reader.readLine() | ||
|
||
new EncryptionInputData(key, text) | ||
} | ||
|
||
@ToString | ||
@TupleConstructor | ||
private static class EncryptionInputData { | ||
String encryptKey | ||
String textToEncrypt | ||
} | ||
} |