Convert text to hash value....
Copy link to clipboard
Copied
Hi All,
I need help to convert set of text into the hash value.
(Paragraph Text + page number) = hash value.
There are several options in web JavaScript but not for InDesign JavaScript. So I am hoping some help here.
Thanks,
Baljeet
Copy link to clipboard
Copied
as in using MD5 or SHA-1?
I have MD5 code if that's what you need
Copy link to clipboard
Copied
Yes, it's likely the same. Is it possible to convert any string value to 30 characters ID value?
Copy link to clipboard
Copied
function MD5(s){function L(k,d){return(k<<d)|(k>>>(32-d))}function K(G,k){var I,d,F,H,x;F=(G&2147483648);H=(k&2147483648);I=(G&1073741824);d=(k&1073741824);x=(G&1073741823)+(k&1073741823);if(I&d){return(x^2147483648^F^H)}if(I|d){if(x&1073741824){return(x^3221225472^F^H)}else{return(x^1073741824^F^H)}}else{return(x^F^H)}}function r(d,F,k){return(d&F)|((~d)&k)}function q(d,F,k){return(d&k)|(F&(~k))}function p(d,F,k){return(d^F^k)}function n(d,F,k){return(F^(d|(~k)))}function u(G,F,aa,Z,k,H,I){G=K(G,K(K(r(F,aa,Z),k),I));return K(L(G,H),F)}function f(G,F,aa,Z,k,H,I){G=K(G,K(K(q(F,aa,Z),k),I));return K(L(G,H),F)}function D(G,F,aa,Z,k,H,I){G=K(G,K(K(p(F,aa,Z),k),I));return K(L(G,H),F)}function t(G,F,aa,Z,k,H,I){G=K(G,K(K(n(F,aa,Z),k),I));return K(L(G,H),F)}function e(G){var Z;var F=G.length;var x=F+8;var k=(x-(x%64))/64;var I=(k+1)*16;var aa=Array(I-1);var d=0;var H=0;while(H<F){Z=(H-(H%4))/4;d=(H%4)*8;aa
Copy link to clipboard
Copied
s is the string to hash. returns 32 character hash
Copy link to clipboard
Copied
Thanks its very helpful. Is it possible to get 27 characters hash instead of 32?
Copy link to clipboard
Copied
Not reliably unique. SHA3-224 will give you 28 characters. You could use a 32 character hash and truncate, but then you would have a potential collision problem.
Just curious why you want 27 characters?
Copy link to clipboard
Copied
Truncating to 27 digits would still be pretty safe with the chances of collision being very low.
To truncate you can use
'123456789012345678901234567890'.substring(0,27)
or
'123456789012345678901234567890'.slice(0,27)
Copy link to clipboard
Copied
I searched over the internet and found below code
function hashCode(myText){
var hash = 0;
if (myText.length == 0) return hash;
for (var i = 0; i < myText.length; i++) {
char1 = myText.charCodeAt(i);
hash = ((hash<<5)-hash)+char1;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
It's generating 10 digit value but I need 30 or 32 digit value.
Copy link to clipboard
Copied
https://forums.adobe.com/people/Baljeet+Singh wrote
(... stuff unrelated to InDesign..)
It's generating 10 digit value but I need 30 or 32 digit value.
I bet you did not read your specifications correctly OR the person who passed them on to you did not understand them.
Copy link to clipboard
Copied
I agree but I need a similar approach to generate 32 characters ID.
Copy link to clipboard
Copied
MD5 creates a 32 character hash. I looked for some native implementation, mentioned below is one such implementation. Look at the algorithm mentioned in the link below. Its a JS code so can be used directly in your script
Javascript MD5 - Javascript tutorial with example source code
-Manan

