哈希到具有给定字符集的字符串

Hash to string with given character set

常用的哈希函数,例如从 digest 创建十六进制输出。我想用给定集合中的字符创建一个散列,例如 [a-z,0-9];不需要强大的加密安全性。

在散列字符串上使用 base64encode 接近,但字符集在该函数中是固定的。

对任意字符 div/mod 的操作很难看 table,所以我决定使用 32 个字符 table 没有 l 0,O

#include <Rcpp.h>
using namespace Rcpp;

static const std::string base32_chars = "abcdefghijkmnpqrstuvwxyz23456789";

// [[Rcpp::export]]
String encode32(uint32_t hash_int, int length = 7)
{
  String res;
  std::ostringstream oss;
  if (length > 7 || length < 1) 
    length = 7;
  for (int i = 0; i < length; i++) {
    oss << base32_chars[hash_int & 31];
    hash_int = hash_int >> 5;
  }
  res = oss.str();
  return res;
}

/*** R
print(encode32(digest::digest2int("Hellod")))
*/