Develop own “Hash” algorithm












0














So I got a integer variable between 1 and 10,000.



I'd like to convert every number to a unique! hash value that has a fixed length and has a custom charset (includes all alphabetic lowercase and uppercase characters).



So:




  • n=10 could get to result="AVduujANNiO"


  • n=4507 could get to result="BciidEPpaEo"





I do not really know how to develop an algorithm like this so this is all I got so far. I think the algorithm should work but of course I get a integer value as hash - not a alphabetic value. No idea how to fix this and how to pad the result to give it a fixed length.



I really hope somebody is able to help me.






let value = "3325";


var getHash = function(value) {
let hash = 0;
for (let i = 0; i < value.length; i++) {
let char = value.charCodeAt(i);
hash = (hash << 6) + char + (char << 14);
hash=-hash
} return hash;
};

console.log(getHash(value))












share|improve this question




















  • 1




    May I ask why you want to do this? It seems pretty wasteful to use an 11 byte string to represent a 14 bit number.
    – hager
    Nov 12 '18 at 20:04








  • 1




    Easy enough, as long as you have at least 10000 possible hash values to choose from. That depends on your fixed length and chosen charset. If your charset includes only the 62 alphanumeric chars then you need at least 3 characters. Will that do? Note that this is "base conversion", not "hashing".
    – Matt Timmermans
    Nov 12 '18 at 20:04






  • 3




    Use your integer to seed a PRNG, then simply use it to fetch N "random" chars from alphabet string.
    – Sergio Tulentsev
    Nov 12 '18 at 20:04








  • 2




    Why are you trying to make your own hash algorithm? (I hope you're not trying to roll your own crypto). Also what properties should your hash function have?
    – Bergi
    Nov 12 '18 at 20:07






  • 4




    I wouldn't call this hashing at all. It sounds like you really want to convert to an arbitrary base using arbitrary characters as digits.
    – TKK
    Nov 12 '18 at 20:20
















0














So I got a integer variable between 1 and 10,000.



I'd like to convert every number to a unique! hash value that has a fixed length and has a custom charset (includes all alphabetic lowercase and uppercase characters).



So:




  • n=10 could get to result="AVduujANNiO"


  • n=4507 could get to result="BciidEPpaEo"





I do not really know how to develop an algorithm like this so this is all I got so far. I think the algorithm should work but of course I get a integer value as hash - not a alphabetic value. No idea how to fix this and how to pad the result to give it a fixed length.



I really hope somebody is able to help me.






let value = "3325";


var getHash = function(value) {
let hash = 0;
for (let i = 0; i < value.length; i++) {
let char = value.charCodeAt(i);
hash = (hash << 6) + char + (char << 14);
hash=-hash
} return hash;
};

console.log(getHash(value))












share|improve this question




















  • 1




    May I ask why you want to do this? It seems pretty wasteful to use an 11 byte string to represent a 14 bit number.
    – hager
    Nov 12 '18 at 20:04








  • 1




    Easy enough, as long as you have at least 10000 possible hash values to choose from. That depends on your fixed length and chosen charset. If your charset includes only the 62 alphanumeric chars then you need at least 3 characters. Will that do? Note that this is "base conversion", not "hashing".
    – Matt Timmermans
    Nov 12 '18 at 20:04






  • 3




    Use your integer to seed a PRNG, then simply use it to fetch N "random" chars from alphabet string.
    – Sergio Tulentsev
    Nov 12 '18 at 20:04








  • 2




    Why are you trying to make your own hash algorithm? (I hope you're not trying to roll your own crypto). Also what properties should your hash function have?
    – Bergi
    Nov 12 '18 at 20:07






  • 4




    I wouldn't call this hashing at all. It sounds like you really want to convert to an arbitrary base using arbitrary characters as digits.
    – TKK
    Nov 12 '18 at 20:20














0












0








0


2





So I got a integer variable between 1 and 10,000.



I'd like to convert every number to a unique! hash value that has a fixed length and has a custom charset (includes all alphabetic lowercase and uppercase characters).



So:




  • n=10 could get to result="AVduujANNiO"


  • n=4507 could get to result="BciidEPpaEo"





I do not really know how to develop an algorithm like this so this is all I got so far. I think the algorithm should work but of course I get a integer value as hash - not a alphabetic value. No idea how to fix this and how to pad the result to give it a fixed length.



I really hope somebody is able to help me.






let value = "3325";


var getHash = function(value) {
let hash = 0;
for (let i = 0; i < value.length; i++) {
let char = value.charCodeAt(i);
hash = (hash << 6) + char + (char << 14);
hash=-hash
} return hash;
};

console.log(getHash(value))












share|improve this question















So I got a integer variable between 1 and 10,000.



I'd like to convert every number to a unique! hash value that has a fixed length and has a custom charset (includes all alphabetic lowercase and uppercase characters).



So:




  • n=10 could get to result="AVduujANNiO"


  • n=4507 could get to result="BciidEPpaEo"





I do not really know how to develop an algorithm like this so this is all I got so far. I think the algorithm should work but of course I get a integer value as hash - not a alphabetic value. No idea how to fix this and how to pad the result to give it a fixed length.



I really hope somebody is able to help me.






let value = "3325";


var getHash = function(value) {
let hash = 0;
for (let i = 0; i < value.length; i++) {
let char = value.charCodeAt(i);
hash = (hash << 6) + char + (char << 14);
hash=-hash
} return hash;
};

console.log(getHash(value))








let value = "3325";


var getHash = function(value) {
let hash = 0;
for (let i = 0; i < value.length; i++) {
let char = value.charCodeAt(i);
hash = (hash << 6) + char + (char << 14);
hash=-hash
} return hash;
};

console.log(getHash(value))





let value = "3325";


var getHash = function(value) {
let hash = 0;
for (let i = 0; i < value.length; i++) {
let char = value.charCodeAt(i);
hash = (hash << 6) + char + (char << 14);
hash=-hash
} return hash;
};

console.log(getHash(value))






javascript string algorithm math hash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 20:11







jonas00

















asked Nov 12 '18 at 19:54









jonas00jonas00

8931619




8931619








  • 1




    May I ask why you want to do this? It seems pretty wasteful to use an 11 byte string to represent a 14 bit number.
    – hager
    Nov 12 '18 at 20:04








  • 1




    Easy enough, as long as you have at least 10000 possible hash values to choose from. That depends on your fixed length and chosen charset. If your charset includes only the 62 alphanumeric chars then you need at least 3 characters. Will that do? Note that this is "base conversion", not "hashing".
    – Matt Timmermans
    Nov 12 '18 at 20:04






  • 3




    Use your integer to seed a PRNG, then simply use it to fetch N "random" chars from alphabet string.
    – Sergio Tulentsev
    Nov 12 '18 at 20:04








  • 2




    Why are you trying to make your own hash algorithm? (I hope you're not trying to roll your own crypto). Also what properties should your hash function have?
    – Bergi
    Nov 12 '18 at 20:07






  • 4




    I wouldn't call this hashing at all. It sounds like you really want to convert to an arbitrary base using arbitrary characters as digits.
    – TKK
    Nov 12 '18 at 20:20














  • 1




    May I ask why you want to do this? It seems pretty wasteful to use an 11 byte string to represent a 14 bit number.
    – hager
    Nov 12 '18 at 20:04








  • 1




    Easy enough, as long as you have at least 10000 possible hash values to choose from. That depends on your fixed length and chosen charset. If your charset includes only the 62 alphanumeric chars then you need at least 3 characters. Will that do? Note that this is "base conversion", not "hashing".
    – Matt Timmermans
    Nov 12 '18 at 20:04






  • 3




    Use your integer to seed a PRNG, then simply use it to fetch N "random" chars from alphabet string.
    – Sergio Tulentsev
    Nov 12 '18 at 20:04








  • 2




    Why are you trying to make your own hash algorithm? (I hope you're not trying to roll your own crypto). Also what properties should your hash function have?
    – Bergi
    Nov 12 '18 at 20:07






  • 4




    I wouldn't call this hashing at all. It sounds like you really want to convert to an arbitrary base using arbitrary characters as digits.
    – TKK
    Nov 12 '18 at 20:20








1




1




May I ask why you want to do this? It seems pretty wasteful to use an 11 byte string to represent a 14 bit number.
– hager
Nov 12 '18 at 20:04






May I ask why you want to do this? It seems pretty wasteful to use an 11 byte string to represent a 14 bit number.
– hager
Nov 12 '18 at 20:04






1




1




Easy enough, as long as you have at least 10000 possible hash values to choose from. That depends on your fixed length and chosen charset. If your charset includes only the 62 alphanumeric chars then you need at least 3 characters. Will that do? Note that this is "base conversion", not "hashing".
– Matt Timmermans
Nov 12 '18 at 20:04




Easy enough, as long as you have at least 10000 possible hash values to choose from. That depends on your fixed length and chosen charset. If your charset includes only the 62 alphanumeric chars then you need at least 3 characters. Will that do? Note that this is "base conversion", not "hashing".
– Matt Timmermans
Nov 12 '18 at 20:04




3




3




Use your integer to seed a PRNG, then simply use it to fetch N "random" chars from alphabet string.
– Sergio Tulentsev
Nov 12 '18 at 20:04






Use your integer to seed a PRNG, then simply use it to fetch N "random" chars from alphabet string.
– Sergio Tulentsev
Nov 12 '18 at 20:04






2




2




Why are you trying to make your own hash algorithm? (I hope you're not trying to roll your own crypto). Also what properties should your hash function have?
– Bergi
Nov 12 '18 at 20:07




Why are you trying to make your own hash algorithm? (I hope you're not trying to roll your own crypto). Also what properties should your hash function have?
– Bergi
Nov 12 '18 at 20:07




4




4




I wouldn't call this hashing at all. It sounds like you really want to convert to an arbitrary base using arbitrary characters as digits.
– TKK
Nov 12 '18 at 20:20




I wouldn't call this hashing at all. It sounds like you really want to convert to an arbitrary base using arbitrary characters as digits.
– TKK
Nov 12 '18 at 20:20












1 Answer
1






active

oldest

votes


















1














Here's a hash function that seems to do what you are asking for :) As a bonus, it offers no collisions til 100,000.






function h(n){
let s = [
'0101000', '1011010', '0011111',
'1100001', '1100101', '1011001',
'1110011', '1010101', '1000111',
'0001100', '1001000'].map(x => parseInt(x, 2));

let m = parseInt('101', 2);

s = s.map(x => {
n ^= m;
m <<= 1;
return (x ^ n) % 52;
});

return s.map(x =>
String.fromCharCode(x > 25 ? 71 + x : 65 + x)
).join('');
}

const s = {};

for (let j=1; j <=10000; j++){
let hash = h(j);

if (s[hash])
console.log('Collision! ' + j + ' <-> ' + s[hash]);

s[hash] = j;
}

console.log('Done. No collisions below 10000.');

for (let j=1; j <11; j++)
console.log(j, h(j));








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269191%2fdevelop-own-hash-algorithm%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Here's a hash function that seems to do what you are asking for :) As a bonus, it offers no collisions til 100,000.






    function h(n){
    let s = [
    '0101000', '1011010', '0011111',
    '1100001', '1100101', '1011001',
    '1110011', '1010101', '1000111',
    '0001100', '1001000'].map(x => parseInt(x, 2));

    let m = parseInt('101', 2);

    s = s.map(x => {
    n ^= m;
    m <<= 1;
    return (x ^ n) % 52;
    });

    return s.map(x =>
    String.fromCharCode(x > 25 ? 71 + x : 65 + x)
    ).join('');
    }

    const s = {};

    for (let j=1; j <=10000; j++){
    let hash = h(j);

    if (s[hash])
    console.log('Collision! ' + j + ' <-> ' + s[hash]);

    s[hash] = j;
    }

    console.log('Done. No collisions below 10000.');

    for (let j=1; j <11; j++)
    console.log(j, h(j));








    share|improve this answer




























      1














      Here's a hash function that seems to do what you are asking for :) As a bonus, it offers no collisions til 100,000.






      function h(n){
      let s = [
      '0101000', '1011010', '0011111',
      '1100001', '1100101', '1011001',
      '1110011', '1010101', '1000111',
      '0001100', '1001000'].map(x => parseInt(x, 2));

      let m = parseInt('101', 2);

      s = s.map(x => {
      n ^= m;
      m <<= 1;
      return (x ^ n) % 52;
      });

      return s.map(x =>
      String.fromCharCode(x > 25 ? 71 + x : 65 + x)
      ).join('');
      }

      const s = {};

      for (let j=1; j <=10000; j++){
      let hash = h(j);

      if (s[hash])
      console.log('Collision! ' + j + ' <-> ' + s[hash]);

      s[hash] = j;
      }

      console.log('Done. No collisions below 10000.');

      for (let j=1; j <11; j++)
      console.log(j, h(j));








      share|improve this answer


























        1












        1








        1






        Here's a hash function that seems to do what you are asking for :) As a bonus, it offers no collisions til 100,000.






        function h(n){
        let s = [
        '0101000', '1011010', '0011111',
        '1100001', '1100101', '1011001',
        '1110011', '1010101', '1000111',
        '0001100', '1001000'].map(x => parseInt(x, 2));

        let m = parseInt('101', 2);

        s = s.map(x => {
        n ^= m;
        m <<= 1;
        return (x ^ n) % 52;
        });

        return s.map(x =>
        String.fromCharCode(x > 25 ? 71 + x : 65 + x)
        ).join('');
        }

        const s = {};

        for (let j=1; j <=10000; j++){
        let hash = h(j);

        if (s[hash])
        console.log('Collision! ' + j + ' <-> ' + s[hash]);

        s[hash] = j;
        }

        console.log('Done. No collisions below 10000.');

        for (let j=1; j <11; j++)
        console.log(j, h(j));








        share|improve this answer














        Here's a hash function that seems to do what you are asking for :) As a bonus, it offers no collisions til 100,000.






        function h(n){
        let s = [
        '0101000', '1011010', '0011111',
        '1100001', '1100101', '1011001',
        '1110011', '1010101', '1000111',
        '0001100', '1001000'].map(x => parseInt(x, 2));

        let m = parseInt('101', 2);

        s = s.map(x => {
        n ^= m;
        m <<= 1;
        return (x ^ n) % 52;
        });

        return s.map(x =>
        String.fromCharCode(x > 25 ? 71 + x : 65 + x)
        ).join('');
        }

        const s = {};

        for (let j=1; j <=10000; j++){
        let hash = h(j);

        if (s[hash])
        console.log('Collision! ' + j + ' <-> ' + s[hash]);

        s[hash] = j;
        }

        console.log('Done. No collisions below 10000.');

        for (let j=1; j <11; j++)
        console.log(j, h(j));








        function h(n){
        let s = [
        '0101000', '1011010', '0011111',
        '1100001', '1100101', '1011001',
        '1110011', '1010101', '1000111',
        '0001100', '1001000'].map(x => parseInt(x, 2));

        let m = parseInt('101', 2);

        s = s.map(x => {
        n ^= m;
        m <<= 1;
        return (x ^ n) % 52;
        });

        return s.map(x =>
        String.fromCharCode(x > 25 ? 71 + x : 65 + x)
        ).join('');
        }

        const s = {};

        for (let j=1; j <=10000; j++){
        let hash = h(j);

        if (s[hash])
        console.log('Collision! ' + j + ' <-> ' + s[hash]);

        s[hash] = j;
        }

        console.log('Done. No collisions below 10000.');

        for (let j=1; j <11; j++)
        console.log(j, h(j));





        function h(n){
        let s = [
        '0101000', '1011010', '0011111',
        '1100001', '1100101', '1011001',
        '1110011', '1010101', '1000111',
        '0001100', '1001000'].map(x => parseInt(x, 2));

        let m = parseInt('101', 2);

        s = s.map(x => {
        n ^= m;
        m <<= 1;
        return (x ^ n) % 52;
        });

        return s.map(x =>
        String.fromCharCode(x > 25 ? 71 + x : 65 + x)
        ).join('');
        }

        const s = {};

        for (let j=1; j <=10000; j++){
        let hash = h(j);

        if (s[hash])
        console.log('Collision! ' + j + ' <-> ' + s[hash]);

        s[hash] = j;
        }

        console.log('Done. No collisions below 10000.');

        for (let j=1; j <11; j++)
        console.log(j, h(j));






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 6:16

























        answered Nov 13 '18 at 3:09









        גלעד ברקןגלעד ברקן

        12.3k21439




        12.3k21439






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269191%2fdevelop-own-hash-algorithm%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()