Skip to content

Instantly share code, notes, and snippets.

View kelvinewilliams's full-sized avatar

Kelvin Williams kelvinewilliams

View GitHub Profile
@kelvinewilliams
kelvinewilliams / gist:5544157
Created May 8, 2013 22:26
Retrieve and return a PGPPrivateKey from an ASCII-Armored block (in this case on disk).
private PGPPrivateKey getPrivateKey(Long keyId, String keyPassphrase) {
PGPSecretKeyRing pgpSecretKeyRing = null;
PGPPrivateKey pgpPrivateKey = null;
PGPSecretKey pgpSecretKey = null;
try {
FileInputStream fis = new FileInputStream("/tmp/sec9.asc");
ArmoredInputStream ais = new ArmoredInputStream(fis);
pgpSecretKeyRing = new PGPSecretKeyRing(ais, new BcKeyFingerprintCalculator());
pgpSecretKey = pgpSecretKeyRing.getSecretKey(keyId);
@kelvinewilliams
kelvinewilliams / createPassphrase
Created May 30, 2013 01:09
Returns 90 character string. Used for passphrases. Replace "[sprinkles]" with characters you want to sprinkle in. :)
private static String createPassphrase() {
long currentTime = System.nanoTime();
char[] x = ("[sprinkles]").toCharArray();
StringBuilder stringBuilder = new StringBuilder();
for (char s : x) {
stringBuilder.append("" + currentTime);
stringBuilder.append(s);
}
try {
@kelvinewilliams
kelvinewilliams / gist:5801113
Created June 17, 2013 22:35
How to always have a Context available... #Android
public class MyApplication {
public static Context ApplicationContext = null;
public static Context getContext() {
return ApplicationContext;
}
}
// In your startup activity set the context.
MyApplication.ApplicationContext = getApplicationContext();
@kelvinewilliams
kelvinewilliams / gist:5945507
Created July 8, 2013 00:37
Simple way to salt passwords. For best results (and consistent sizes for database storage) hash the resulting string.
private static String saltIt(String passphrase) {
String saltChars = "i08q4h0fn23in2034qnvosysag01n40!$#@#$^@#$#ESDGASFXBSDFHDFH2";
char[] salt = saltChars.toCharArray();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(passphrase);
for (char s : salt) {
stringBuilder.append(s);
stringBuilder.append(passphrase);
}
@kelvinewilliams
kelvinewilliams / gist:6270514
Last active December 21, 2015 07:19
Java: DOM Check a Document (org.w3c.dom.Document) for the presence of a tag.
private boolean hasTag(Document document, String tag) {
NodeList nodeList = document.getElementsByTagName(tag);
return (nodeList.getLength() > 0);
}
@kelvinewilliams
kelvinewilliams / InputStreamReading.java
Last active December 23, 2015 02:29
Java: Reading InputStreams
/* One way to read an InputStream... NOT Recommended when dealing with some InputStreams, especially if that is from a network connection. */
try {
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
// do something with the bytes read from the InputStream
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
@kelvinewilliams
kelvinewilliams / Reports.java
Created September 18, 2013 20:04
Reusable class for generating DOM from a ResultSet
package net.kelvinwilliams.utils
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
@kelvinewilliams
kelvinewilliams / OracleDDLDumper.java
Last active December 23, 2015 18:59
Java: Dumps Oracle database objects' DDL to text files (great for automatically committing changes to Git).
package [your package name here]
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
/**
* Created with IntelliJ IDEA.
@kelvinewilliams
kelvinewilliams / gist:6879927
Created October 8, 2013 05:35
In every voice network I've ever logged into I've found "friendly-scanner" being "friendly." Protect your networks.
U 50.30.37.10:5966 -> 10.184.33.102:5060
REGISTER sip:12345d@50.17.231.189 SIP/2.0.
Via: SIP/2.0/UDP 62.75.212.215:5966;branch=z9hG4bK-4195776885;rport.
Content-Length: 0.
From: "12345d"<sip:12345d@50.17.231.189>; tag=3132333435640133313735313839333631.
Accept: application/sdp.
User-Agent: friendly-scanner.
To: "12345d"<sip:12345d@50.17.231.189>.
Contact: sip:12345d@50.17.231.189.
CSeq: 1 REGISTER.
@kelvinewilliams
kelvinewilliams / uncommitted_transactions.sql
Created October 28, 2013 02:16
Uncomitted Transactions
create view uncommitted_transactions as
select t.start_time,s.sid,s.serial#,s.username,s.status,s.schemaname,
s.osuser,s.process,s.machine,s.terminal,s.program,s.module,to_char(s.logon_time,'DD/MON/YY HH24:MI:SS') logon_time
from v$transaction t, v$session s
where s.saddr = t.ses_addr
order by start_time;