Skip to content

Instantly share code, notes, and snippets.

@lukateras
Created April 18, 2019 23:02
Show Gist options
  • Save lukateras/a4d4fbca541413dd977ad308a6d5a290 to your computer and use it in GitHub Desktop.
Save lukateras/a4d4fbca541413dd977ad308a6d5a290 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
using namespace std;
unsigned int hashLength32(string hash) {
return (hash.length() * 8 - 1) / 5 + 1;
}
// omitted: E O U T
const string base32Chars = "0123456789abcdfghijklmnpqrsvwxyz";
string printHash32(string hash) {
unsigned int len = hashLength32(hash);
string s;
s.reserve(len);
for (int n = len - 1; n >= 0; n--) {
unsigned int b = n * 5;
unsigned int i = b / 8;
unsigned int j = b % 8;
unsigned char c =
(hash[i] >> j)
| (i >= hash.length() - 1 ? 0 : hash[i + 1] << (8 - j));
s.push_back(base32Chars[c & 0x1f]);
}
return s;
}
int main() {
for (string line; std::getline(cin, line);) {
cout << printHash32(line) << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment