Skip to content

Instantly share code, notes, and snippets.

@jmhertlein
jmhertlein / gist:5471873
Created April 27, 2013 04:20
pseudocode for authenticating a user with rsa keys
Server has client's public key already, in a list of trusted public keys
keys are RSA key pairs
client connects
server sends its pubkey
client now using server's pubkey to encrypt all outgoing communications, incoming are still unencrypted
client sends username it wants to authenticate as
server loads trusted pubkey for username
encrypts n securely-randomly-generated bytes, sends over unencrypted connection to client
client uses private key to decrypt
@jmhertlein
jmhertlein / gist:4123956
Created November 21, 2012 09:23
Create a binary search tree from a list of numbers sorted in increasing order
//Precondition: A is a list of elements, sorted in increasing order
// P is the root of the binary tree to be created
//Postcondition: P is the root of a binary tree containing all elements in A
procedure mktree(list A, node P)
if A is empty,
return
middle = A.length/2
buildMaxHeap(data)
//Precondition: data is an ordered set that contains n elements
data.heapSize = data.length
//Loop invariant: the trees rooted at indices in the range [i,n] are max-heaps
for i=FLOOR(data.length/2) downto 1
maxHeapify(data, I)
//Postcondition: data[1] is the root of a tree that is a max-heap
mergeSort(data)
//Precondition: data is an ordered set of n elements
@jmhertlein
jmhertlein / gist:3327775
Created August 11, 2012 23:20
procedure: serverCountdownTask
var remainingTime := 5
procedure run()
if(remainingTime <= 0) then
Bukkit.shutdown()
else
print("Halt in " + remainingTime)
remainingTime--
end
@jmhertlein
jmhertlein / listeners.java
Created June 29, 2012 23:41
Listeners (CraftBukkit)
//let's say we're replacing this:
public MyChatListener extends ChatListener {
public void onPlayerChat(PlayerChatEvent event) {
//do something
}
}
//We'll change it to this:
@jmhertlein
jmhertlein / removefirstocc.cpp
Created February 27, 2012 23:39
Removing the first occurrence of a generic element x in a node-based linked list.
//assume we're looking for x.
bool found = false;
Node* temp = head, rm = NULL;
//now we loop through our list looking for a node that holds something == to x
while(temp != null && !found) {
if(temp->data == x) { //if we find it, set found to true and rm to the node
found = true;
rm = temp;