0

The usual hash-functions, e.g. from digest create hex output. I want to create a hash with character from a given set, e.g [a-z,0-9]; no strong cryptographic security is required.

Using base64encode on a hashed string comes close, but the character set is fixed in that function.

2
  • On my system, digest("anton") gives "1b17325d9c286f0187efcfbd7de6b74e" which is hex. I want a shorter string that in addition avoids some easily confused characters like "l" and O/0 for easier mnemonics. Commented Oct 30, 2021 at 10:16
  • Of course, sorry. my mistake Commented Oct 30, 2021 at 10:18

1 Answer 1

1

It is ugly div/mod manipulation for an arbitrary character table, so I decided to use a 32 character table without 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")))
*/
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.