Rewrite nested_hash_from_key as a recursive function

Instead of the original iterative apporach using local state as the position in
the tree, generate the hash via a recursive call. Simpler to understand, I
think.