Skip to content

Instantly share code, notes, and snippets.

View ansig's full-sized avatar

Anders Sigfridsson ansig

View GitHub Profile
@ansig
ansig / PBKDF2PasswordHasher.java
Created November 8, 2014 07:44
Hash a password using PBKDF2 with hman and sha1 algorithm.
import com.sun.org.apache.xml.internal.security.utils.Base64;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
/**
* Hash a password using PBKDF2 with hman and sha1 algorithm.
@ansig
ansig / AESEncryptorDecryptor.java
Created November 8, 2014 08:18
Encrypting and decrypting a text using a passphrase and AES. The encryption key is generated from a passphrase using PBKDF2.
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
@ansig
ansig / WaitNotifyMechanism.java
Created November 9, 2014 19:14
Classes that produce and consume data from a shared store to demonstrate the wait/notify mechanism.
import java.util.ArrayList;
/**
* Classes that produce and consume data from a shared store to demonstrate the wait/notify mechanism.
*/
public class WaitNotifyMechanism {
static ArrayList<Integer> dataStore = new ArrayList<Integer>();
static class Producer extends Thread {
@ansig
ansig / gist:10ada9c734a965fbc6f5
Last active January 30, 2016 06:56
Make class functions callable via command line
import sys
class MyClass(object):
def func1(self, arg1):
print "Invoked: func1({0})".format(arg1)
def func2(self, arg1, arg2):
print "Invoked: func2({0}, {1})".format(arg1, arg2)
def main(args):
@ansig
ansig / FileFinder.java
Created November 16, 2014 20:07
Traverse a directory tree with a FileVisitor and print every file that matches a Glob pattern.
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
/**
* Traverse a directory and print every file that matches a Glob pattern. In this example, the users home dir is
* searched for .java and .py files.
*/
public class FileFinder extends SimpleFileVisitor<Path> {
@ansig
ansig / list_to_dict_with_subtitle.py
Last active January 30, 2016 06:36
Create a dict from two lists with items from second list under a subtitle
list1 = ['item1', 'item2', 'item3']
list2 = ['subitem1', 'subitem2', 'subitem3']
mapped_dict = {x: {"subtitle" :y} for x, y in zip(list1, list2)}
@ansig
ansig / ConsoleInput.java
Created November 18, 2014 19:07
Take in username and password from the console.
import java.io.Console;
/**
* Take in username and password from the console.
*/
public class ConsoleInput {
public static void main(String[] args) {
Console console = System.console();
@ansig
ansig / DirWatcher.java
Created November 18, 2014 19:57
Watch a directory for changes using a WatchService.
import java.io.IOException;
import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.*;
/**
* Watch a directory for changes using a WatchService.
*/
public class DirWatcher {
@ansig
ansig / ThreadPool.java
Created November 27, 2014 19:26
A thread pool with the executor framework to execute tasks. Also demonstrates a simple timeout mechanism.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* A thread pool with the executor framework to execute tasks. Also demonstrates a simple timeout mechanism.
*/
public class ThreadPool {
private static final int NTHREADS = Runtime.getRuntime().availableProcessors();
@ansig
ansig / CallableFuture.java
Last active August 29, 2015 14:10
Use a callable object to retrieve values from tasks executed in a thread pool.
import java.util.concurrent.*;
/**
* Use a callable to retrieve values from tasks executed in a thread pool.
*/
public class CallableFuture {
public static void main(String[] args) {
Callable<Long> task = new Factorial(20);