0

I need to generate a substirng that should works as an unique identifier (something like an hash), so with a reduced risk of collisions, for a string in JavaScript. This Hash/Substring however should not exceed 32 bytes of length .

I need this because I've to store it in a Solidity Smart Contract as identifier of that string. I've found many functions to get and Hash code from a string in JavaScript, however is not what I'm looking for.

         String.prototype.hashCode = function() {
            var hash = 0,
                i, chr;
            if (this.length === 0) return hash;
            for (i = 0; i < this.length; i++) {
                chr = this.charCodeAt(i);
                hash = ((hash << 5) - hash) + chr;
                hash |= 0; // Convert to 32bit integer
            }
            return hash;
        };

If I have a string like this: "Hello, this is a smart question", I expect an hash that represent the string, for example "hllhsmrtqstin". Could be even smaller then 32 bytes but it's important that does not exeed this lenght, regardless string size.

N.B. I'm not looking for something like sha256, because I need a human readbale hash that could be reconduced to the original string, another example "Hello World" > "hlwld"

Philip
  • 771
  • 1
  • 9
  • 24
  • https://www.npmjs.com/package/js-sha256 ? – e.dan Apr 18 '19 at 08:11
  • This answer returns the format you wish: https://stackoverflow.com/a/43383990/3702797 – Kaiido Apr 18 '19 at 08:13
  • Sorry @Kaiido but I don't think it's duplicated, Sha256 does not meet my requirements. Indeed hash of "hello world" produce " b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" very different for what I'm looking for. Please check the example in the question, I need an human readble hash meaningful for the string hashed. Another example cold be "Hello World" => "hlwld", and again It's not important that is not 32 bytes length, but has to be a max length of 32 byets. Thank you. – Philip Apr 18 '19 at 08:30
  • Then that's no hash but substrings and your requirement of no collision is impossible. `aaaaaaaaaaaaaa...(×64)` will produce the same as `aaaaaaaaaaaaaa...(×128)` – Kaiido Apr 18 '19 at 08:34
  • If you edit your question to not ask for a hash, I'll remove the dupe closure, but you won't find your happiness I'm afraid – Kaiido Apr 18 '19 at 08:37
  • Thanks I've edit the question, I know that no collisions are impossible, I'm looking for the best collision resistent for my requirementes, of course this cannot be compared to sha256 – Philip Apr 18 '19 at 08:37

1 Answers1

0

You could replace some items, like

  • is
  • aeou
  • all non letters
  • t following by an h character.
Hello, this is a smart question
h ll    h        smr t q  sti n

var string = 'Hello, this is a smart question'
    result = string.toLowerCase().replace(/is|[aeou]|[^a-z]|t(?=h)/g, '');
   
console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392