MD5 hash from file in C++

Here’s a straight forward implementation of the md5sum command that computes and displays the MD5 of the file specified on the command-line. It needs to be linked against the OpenSSL library (gcc md5.c -o md5 -lssl) to work. It’s pure C, but you should be able to adapt it to your C++ application easily enough. … Read more

Multidimensional associative arrays in Bash

You can’t do what you’re trying to do: bash arrays are one-dimensional $ declare -A PERSONS $ declare -A PERSON $ PERSON[“FNAME”]=’John’ $ PERSON[“LNAME”]=’Andrew’ $ declare -p PERSON declare -A PERSON='([FNAME]=”John” [LNAME]=”Andrew” )’ $ PERSONS[1]=([FNAME]=”John” [LNAME]=”Andrew” ) bash: PERSONS[1]: cannot assign list to array member You can fake multidimensionality by composing a suitable array index … Read more

How do I compare two hashes?

You can compare hashes directly for equality: hash1 = {‘a’ => 1, ‘b’ => 2} hash2 = {‘a’ => 1, ‘b’ => 2} hash3 = {‘a’ => 1, ‘b’ => 2, ‘c’ => 3} hash1 == hash2 # => true hash1 == hash3 # => false hash1.to_a == hash2.to_a # => true hash1.to_a == hash3.to_a … Read more

Given a number, produce another random number that is the same every time and distinct from all other results

This sounds like a non-repeating random number generator. There are several possible approaches to this. As described in this article, we can generate them by selecting a prime number p and satisfies p % 4 = 3 that is large enough (greater than the maximum value in the output range) and generate them this way: … Read more

Initialize Java Generic Array of Type Generic

Generics in Java doesn’t allow creation of arrays with generic types. You can cast your array to a generic type, but this will generate an unchecked conversion warning: public class HashTable<K, V> { private LinkedList<V>[] m_storage; public HashTable(int initialSize) { m_storage = (LinkedList<V>[]) new LinkedList[initialSize]; } } Here is a good explanation, without getting into … Read more