Page 1 of 1

javascript function to Encode the resulting 16 byte string

Posted: Sun Jan 17, 2016 8:30 am
by joseggarza
can someone help me translate this javascript into livecode:

Code: Select all

const ascii64 =
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

function to64(v, n)
{
 var s = "";
 while (--n >= 0) {
  s += ascii64.charAt( v & 0x3f );
  v >>= 6;
 }
 return s;
}



function to64_triplet(str, idx0, idx1, idx2)
{
 var v = (str.charCodeAt(idx0) << 16) |
         (str.charCodeAt(idx1) <<  8) |
         (str.charCodeAt(idx2));
 return to64(v, 4);
}



function to64_single(str, idx0)
{
 var v = str.charCodeAt(idx0);
 return to64(v, 2);
}
I need this functions to finish the last section of (MD5 Crypt) module

Re: javascript function to Encode the resulting 16 byte stri

Posted: Thu Jan 21, 2016 12:29 pm
by Lagi Pittas
Hi

Unless I'm missing something this should do it.

Code: Select all

Function to64  v, n
    local lcStr
    put character 1 to n of v into lcStr
    return Base64Encode(lcStr)
end to64

function to64_single v, idx0
       return Base64Encode(character idx0 of v)
end to64_single

function to64_triplet v, idx0, idx1, idx2
       local lcStr

         put character idx0 of v after lcStr
         put character idx1 of v after lcStr
         put character idx2  of v after lcStr

       return Base64Encode(lcStr)
end to64_triplet
Kindest Regards Lagi

p.s. looking at the code again you might need to fiddle with the "n" character by leaving it out if you pass onluy the actual string you want encoded. Base64 is about a third bigger than the original string so 1 character becomes 2 and 3 become 4. So i Don't know if the function is base64 encoding twice by the single and triple calling the to_64 function with a length parameter.Base64encode in LC takes all this into account.
Testing to_64("Mannatoga",3) gives "TWFu" and callings To64_triple("XXM..a.n.natoga",3,6,8) gives the same result

Re: javascript function to Encode the resulting 16 byte stri

Posted: Wed Jan 27, 2016 1:49 am
by joseggarza
Hi Lagi Pittas,

Thanks for your time, the full outcome is to make md5Crypt function.
Using openSSL this is what I get:

Code: Select all

C:\>openssl passwd -1 -salt salt password
$1$salt$qJH7.N4xYta3aEG/dfqo/0
that actually means encrypt "password" with Salt "salt" and the result is ---> $1$salt$qJH7.N4xYta3aEG/dfqo/0

I found a javascript to do just that...

Code: Select all



const ascii64 =
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

function to64(v, n)
{
 var s = "";
 while (--n >= 0) {
  s += ascii64.charAt( v & 0x3f );
  v >>= 6;
 }
 return s;
}

function to64_triplet(str, idx0, idx1, idx2)
{
 var v = (str.charCodeAt(idx0) << 16) |
         (str.charCodeAt(idx1) <<  8) |
         (str.charCodeAt(idx2));
 return to64(v, 4);
}

function to64_single(str, idx0)
{
 var v = str.charCodeAt(idx0);
 return to64(v, 2);
}

function md5crypt(password, salt)
{
 var ctx = password+"$1$"+salt;
 var ctx1 = str_md5( password+salt+password );

 /* "Just as many characters of ctx1" (as there are in the password) */
 for (var pl=password.length; pl>0; pl -= 16)
  ctx += ctx1.slice(0, (pl > 16) ? 16 : pl);

 /* "Then something really weird" */
 for (var i=password.length; i != 0; i >>= 1)
  if (i & 1)
   ctx += "\0";
  else
   ctx += password.charAt(0);

 ctx = str_md5(ctx);

 /* "Just to make sure things don't run too fast" */
 for (i=0; i<1000; i++) {
  ctx1 = "";
  if (i & 1) ctx1 += password;
  else ctx1 += ctx;

  if (i % 3) ctx1 += salt;

  if (i % 7) ctx1 += password;

  if (i & 1) ctx1 += ctx;
  else ctx1 += password;

  ctx = str_md5(ctx1);
 }

 return "$1$" + salt + "$" +
   to64_triplet(ctx, 0,  6, 12) +
   to64_triplet(ctx, 1,  7, 13) +
   to64_triplet(ctx, 2,  8, 14) +
   to64_triplet(ctx, 3,  9, 15) +
   to64_triplet(ctx, 4, 10,  5) +
   to64_single(ctx, 11);
}


I tried to do half but I did not know how to do bit-shit on LC, an you did the rest.... this is what I got mixing with your code
function str_md5 hashtext
local md5, mdhex
put md5Digest(hashtext) into md5
get binaryDecode("H*",md5,mdhex)
return mdhex
end str_md5

Function to64 v, n
local lcStr
put character 1 to n of v into lcStr
return Base64Encode(lcStr)
end to64

function to64_single v, idx0
return Base64Encode(character idx0 of v)
end to64_single

function to64_triplet v, idx0, idx1, idx2
local lcStr

put character idx0 of v after lcStr
put character idx1 of v after lcStr
put character idx2 of v after lcStr

return Base64Encode(lcStr)
end to64_triplet

function md5crypt pPassword, pSalt
local ctx, ctx1
put pPassword & "$1$" & pSalt into ctx
put str_md5(pPassword & pSalt & pPassword) into ctx1
put the length of pPassword into pl


/* "Just as many characters of ctx1" (as there are in the password) */
repeat while pl > 0
if pl > 16 then
put 16 into tMax
put char 1 to tMax of ctx1 after ctx
else
put pl into tMax
put char 1 to tMax of ctx1 after ctx
end if
subtract 16 from pl
end repeat


/* "Then something really weird" */
put numToChar(the length of pPassword) into i
if the length of pPassword >0 then

--put numToChar(i) into temp
--put temp into fld"temp"
get binaryDecode("b*",i,binary_i)
put binary_i into fld "temp"
repeat for each character tChar in binary_i
if tChar = 1 then
put char 1 of pPassword after ctx
else
put null after ctx
end if
put str_md5(ctx) into ctx
end repeat

end if

/* "Just to make sure things don't run too fast" */
put 0 into i
repeat while i<1000
put "" into ctx1

if (i mod 2) = 1 then
put pPassword after ctx1
else
put ctx after ctx1
end if

if (i mod 3) <>0 then
put pSalt after ctx1
end if

if (i mod 7) <> 0 then
put pPassword after ctx1
end if

if (i mod 2) = 0 then
put ctx after ctx1
else
put pPassword after ctx1
end if

put str_md5(ctx1) into ctx
add 1 to i
end repeat

put "" into new_ctx
put char 25 to 26 of ctx after new_ctx
put char 13 to 14 of ctx after new_ctx
put char 1 to 2 of ctx after new_ctx
put char 27 to 28 of ctx after new_ctx
put char 15 to 16 of ctx after new_ctx
put char 3 to 4 of ctx after new_ctx
put char 29 to 30 of ctx after new_ctx
put char 17 to 18 of ctx after new_ctx
put char 5 to 6 of ctx after new_ctx
put char 31 to 32 of ctx after new_ctx
put char 19 to 20 of ctx after new_ctx
put char 7 to 8 of ctx after new_ctx
put char 11 to 12 of ctx after new_ctx
put char 21 to 22 of ctx after new_ctx
put char 9 to 10 of ctx after new_ctx
put char 23 to 24 of ctx after new_ctx


return "$1$" & pSalt & "$" & \
to64_triplet (ctx, 0, 6, 12) & \
to64_triplet(ctx, 1, 7, 13) & \
to64_triplet(ctx, 2, 8, 14)& \
to64_triplet(ctx, 3, 9, 15)& \
to64_triplet(ctx, 4, 10, 5) & \
to64_single(ctx, 11)
end md5crypt
I attached the test md5crypt.....
I also found the algorithm for it on : https : // pythonhosted . org/passlib/lib/passlib.hash.md5_crypt.html#algorithm

Can you check if you can find the problem?

Re: javascript function to Encode the resulting 16 byte stri

Posted: Fri Jan 29, 2016 7:51 pm
by Lagi Pittas
HI Jose

Checked the routines just before the

"Then something really weird"

and they return what Is expected.

but here

Code: Select all

--put numToChar(i) into temp
--put temp into fld"temp"
get binaryDecode("b*",i,binary_i) 
put binary_i into fld "temp"
repeat for each character tChar in binary_i
if tChar = 1 then
put char 1 of pPassword after ctx
else
put null after ctx
end if
put str_md5(ctx) into ctx
end repeat
the str_md5 call ...

Code: Select all

str_md5(ctx) into ctx
should be outside the repeat loop as the if is a single statement not a compound statement using braces.

Code: Select all

   /* "Then something really weird" */
   
   put numToChar(the length of pPassword) into i
   if the length of pPassword >0 then
      
   --put numToChar(i) into temp
   --put temp into fld"temp"
   get binaryDecode("b*",i,binary_i) 
   put binary_i into fld "temp"
   repeat for each character tChar in binary_i
      if tChar = 1 then
         put char 1 of pPassword after ctx
      else
         put null after ctx
      end if
   end repeat

  put str_md5(ctx) into ctx --- moved out of repeat loop
Hope that helps

Lagi