Skip to content

Instantly share code, notes, and snippets.

@asaph
asaph / getTOTPCode.java
Created April 25, 2016 03:40
Calculate the 6-digit 2fa code based on base32 encoded secret key and the current time
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");
}
@asaph
asaph / getRandomSecretKey.java
Created April 25, 2016 03:34
Generate a random Google Authenticator compatible 20 byte base32 encoded secret key
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 ");
}
@asaph
asaph / pom.xml
Created April 25, 2016 03:29
Commons Codec Maven Dependency
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
@asaph
asaph / import_csv_into_mysql_table.sql
Created July 6, 2015 21:18
Import a CSV file into a MySQL table ignoring header line
load data infile '/tmp/file.csv'
into table my_table
fields terminated by ','
optionally enclosed by '"'
escaped by '"'
lines terminated by '\n'
ignore 1 lines;
@asaph
asaph / sql2tsv.sh
Created June 2, 2015 20:27
Generate a TSV spreadsheet from a SQL command
#!/bin/sh
mysql databasename -h hostname -u username -p -B -e "select * from mytable;" > /tmp/myreport.tsv
@asaph
asaph / generate_self_signed_cert.sh
Created June 2, 2015 20:04
Generate Self-Signed Cert in JKS format using Java's keytool
#!/bin/sh
keytool -genkey -validity 365 -alias "youralias" -keyalg "RSA" -keystore .keystore -keypass "yourkeypass" -storepass "yourstorepass" -dname "CN=localhost, OU=, O=, L=, S=, C="
@asaph
asaph / generate_self_signed_cert.sh
Created June 2, 2015 19:59
Generate Self-Signed Cert in PEM format using OpenSSL
#!/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