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
#!/bin/sh | |
openssl req -new -newkey rsa:2048 -nodes -days 365 -x509 -subj "/C=/ST=/L=/O=/CN=localhost" -keyout privkey.pem -out cacert.pem |
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
#!/bin/sh | |
keytool -genkey -validity 365 -alias "youralias" -keyalg "RSA" -keystore .keystore -keypass "yourkeypass" -storepass "yourstorepass" -dname "CN=localhost, OU=, O=, L=, S=, C=" |
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
#!/bin/sh | |
mysql databasename -h hostname -u username -p -B -e "select * from mytable;" > /tmp/myreport.tsv |
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
<dependency> | |
<groupId>commons-codec</groupId> | |
<artifactId>commons-codec</artifactId> | |
<version>1.10</version> | |
</dependency> |
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 static String getRandomSecretKey() { | |
SecureRandom random = new SecureRandom(); | |
byte[] bytes = new byte[20]; | |
random.nextBytes(bytes); | |
Base32 base32 = new Base32(); | |
String secretKey = base32.encodeToString(bytes); | |
// make the secret key more human-readable by lower-casing and | |
// inserting spaces between each group of 4 characters | |
return secretKey.toLowerCase().replaceAll("(.{4})(?=.{4})", "$1 "); | |
} |
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 static String getTOTPCode(String secretKey) { | |
String normalizedBase32Key = secretKey.replace(" ", "").toUpperCase(); | |
Base32 base32 = new Base32(); | |
byte[] bytes = base32.decode(normalizedBase32Key); | |
String hexKey = Hex.encodeHexString(bytes); | |
long time = (System.currentTimeMillis() / 1000) / 30; | |
String hexTime = Long.toHexString(time); | |
return TOTP.generateTOTP(hexKey, hexTime, "6"); | |
} |
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
String secretKey = "quu6 ea2g horg md22 sn2y ku6v kisc kyag"; | |
String lastCode = null; | |
while (true) { | |
String code = getTOTPCode(secretKey); | |
if (!code.equals(lastCode)) { | |
// output a new 6 digit code | |
System.out.println(code); | |
} | |
lastCode = code; | |
try { |
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
<dependency> | |
<groupId>com.google.zxing</groupId> | |
<artifactId>javase</artifactId> | |
<version>3.2.1</version> | |
</dependency> |
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 static String getGoogleAuthenticatorBarCode(String secretKey, String account, String issuer) { | |
String normalizedBase32Key = secretKey.replace(" ", "").toUpperCase(); | |
try { | |
return "otpauth://totp/" | |
+ URLEncoder.encode(issuer + ":" + account, "UTF-8").replace("+", "%20") | |
+ "?secret=" + URLEncoder.encode(normalizedBase32Key, "UTF-8").replace("+", "%20") | |
+ "&issuer=" + URLEncoder.encode(issuer, "UTF-8").replace("+", "%20"); | |
} catch (UnsupportedEncodingException e) { | |
throw new IllegalStateException(e); | |
} |
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 static void createQRCode(String barCodeData, String filePath, int height, int width) | |
throws WriterException, IOException { | |
BitMatrix matrix = new MultiFormatWriter().encode(barCodeData, BarcodeFormat.QR_CODE, | |
width, height); | |
try (FileOutputStream out = new FileOutputStream(filePath)) { | |
MatrixToImageWriter.writeToStream(matrix, "png", out); | |
} | |
} |
OlderNewer