Develop own “Hash” algorithm
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 toresult="AVduujANNiO"
n=4507
could get toresult="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))
javascript string algorithm math hash
|
show 10 more comments
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 toresult="AVduujANNiO"
n=4507
could get toresult="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))
javascript string algorithm math hash
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
|
show 10 more comments
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 toresult="AVduujANNiO"
n=4507
could get toresult="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))
javascript string algorithm math hash
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 toresult="AVduujANNiO"
n=4507
could get toresult="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
javascript string algorithm math hash
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
|
show 10 more comments
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
|
show 10 more comments
1 Answer
1
active
oldest
votes
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));
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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));
add a comment |
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));
add a comment |
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));
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));
edited Nov 13 '18 at 6:16
answered Nov 13 '18 at 3:09
גלעד ברקןגלעד ברקן
12.3k21439
12.3k21439
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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