Last active
October 15, 2016 20:56
-
-
Save davoclavo/7460039 to your computer and use it in GitHub Desktop.
tool to unhash/hash vine ids
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Thanks you for this explication,
You inspire me to do the same thing for PHP,fo my use 😄
function unhash_id($hashedId){
$vineKey = array_flip(str_split('BuzaW7ZmKAqbhMOei5J1nvr6gXHwdpDjITtFUPxQ20E9VY3Ll'));
$vineKeySize = count($vineKey);
$arrayHashedId = str_split(strrev($hashedId));
$unhashId = 0;
foreach($arrayHashedId as $key => $value){
$unhashId += $vineKey[$value] * pow($vineKeySize, $key);
}
return $unhashId;
}
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
https://vine.co/v/hhpQVXFvA91