Skip to content

Instantly share code, notes, and snippets.

@davoclavo
Last active October 15, 2016 20:56
Show Gist options
  • Save davoclavo/7460039 to your computer and use it in GitHub Desktop.
Save davoclavo/7460039 to your computer and use it in GitHub Desktop.
tool to unhash/hash vine ids
module VineHasher
VINE_KEY = 'BuzaW7ZmKAqbhMOei5J1nvr6gXHwdpDjITtFUPxQ20E9VY3Ll'
VINE_KEY_SIZE = VINE_KEY.size
VINE_KEY_HASH = VINE_KEY.split('').each_with_index.inject({}) {|hash, (key, index)| hash[key] = index; hash }
def unhash_id(hashed_id)
hashed_id.reverse.split('').each_with_index.inject(0) { |total, (key, index)| total + VINE_KEY_HASH[key] * VINE_KEY_SIZE**index }
end
def hash_id(id)
id.to_base(VINE_KEY_SIZE).map{|n| VINE_KEY[n]}.join()
end
class ::Integer
def to_base(base=10)
return [0] if zero?
raise ArgumentError, 'base must be greater than zero' unless base > 0
num = abs
return [1] * num if base == 1
[].tap do |digits|
while num > 0
digits.unshift num % base
num /= base
end
end
end
end
end
@juanfausd
Copy link

Thanks for the explanation! here there is a console app that calculates the hash_id in C#:

class Program
{
    static void Main(string[] args)
    {
        string VINE_KEY = "BuzaW7ZmKAqbhMOei5J1nvr6gXHwdpDjITtFUPxQ20E9VY3Ll";
        int VINE_KEY_SIZE = VINE_KEY.Length;
        string postId = "1202036007948943360";
        string base49PostId = Encode(long.Parse(postId), VINE_KEY_SIZE, VINE_KEY);

        Console.WriteLine(base49PostId);
        Console.ReadLine();
    }

    public static string Encode(long value, int @base = 0, string chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    {
        if (@base <= 0) @base = chars.Length;
        var sb = new StringBuilder();
        do
        {
            int m = (int)(value % @base);
            sb.Insert(0, chars[m]);
            value = (value - m) / @base;
        } while (value > 0);
        return sb.ToString();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment