|
-
Nov 5th, 2008, 08:28 PM
#1
Thread Starter
Member
Convert Javascript to work in vb6.0
How can I use the microsoft script control to get the result of this java script to show in a text box?
Code:
<script>
function hex_sha1(s)
{
return binb2hex(core_sha1(str2binb(s),s.length*8));
}
function core_sha1(x, len)
{
x[len>>5]|=0x80<<(24-len%32);
x[((len+64>>9)<<4)+15]=len;
var w=Array(80);
var a=1732584193;
var b= -271733879;
var c= -1732584194;
var d=271733878;
var e= -1009589776;
for(var i=0;i<x.length;i+=16)
{
var olda=a;
var oldb=b;
var oldc=c;
var oldd=d;
var olde=e;
for(var j=0;j<80;j++)
{
if(j<16)
{
w[j]=x[i+j];
}
else
{
w[j]=((w[j-3]^w[j-8]^w[j-14]^w[j-16])<<1)|((w[j-3]^w[j-8]^w[j-14]^w[j-16])>>>(32-1));
}
var y2=(j<20)?1518500249:(j<40)?1859775393:(j<60)? -1894007588: -899497514;
var t=safe_add(safe_add((a<<5)|(a>>>(32-5)),sha1_ft(j,b,c,d)),safe_add(safe_add(e,w[j]),y2));
e=d;
d=c;
c=(b<<30)|(b>>>(32-30));
b=a;
a=t;
}
a=safe_add(a,olda);
b=safe_add(b,oldb);
c=safe_add(c,oldc);
d=safe_add(d,oldd);
e=safe_add(e,olde);
}
return Array(a,b,c,d,e);
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
function sha1_ft(t,b,c,d)
{
if(t<20)
{
return(b&c)|((~b)&d);
}
if(t<40)
{
return b^c^d;
}
if(t<60)
{
return(b&c)|(b&d)|(c&d);
}
return b^c^d;
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
function safe_add(x,y){return((x>>16)+(y>>16)+((x&0xFFFF)+(y&0xFFFF)>>16)<<16)|((x&0xFFFF)+(y&0xFFFF)&0xFFFF);}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
function str2binb(str)
{
var bin=Array();
var mask=(1<<8)-1;
for(var i=0; i<str.length*8; i+=8)
{
bin[i>>5]|=(str.charCodeAt(i/8)&mask)<<(32-8-i%32);
}
return bin;
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
function binb2hex(binarray)
{
var hex_tab=0?"0123456789ABCDEF":"0123456789abcdef";
var str="";
for(var i=0; i<binarray.length*4; i++)
{
str+=hex_tab.charAt((binarray[i>>2]>>((3-i%4)*8+4))&0xF)+hex_tab.charAt((binarray[i>>2]>>((3-i%4)*8))&0xF);
}
return str;
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
hc=0+parseInt(1234)%123431+parseInt(1234)%123431;
///////////////////////////////////////////////////
///////////////////////////////////////////////////
document.write(hex_sha1(""+hc));
</script>
Last edited by VbCoder2008; Nov 11th, 2008 at 08:59 PM.
Reason: Javascript not vb
-
Nov 5th, 2008, 11:40 PM
#2
Frenzied Member
Re: Convert php script to work in vb6.0
Way too advanced PHP for me, sry
-
Nov 5th, 2008, 11:46 PM
#3
Re: Convert php script to work in vb6.0
Looks more like Java Script. Why do you call it PHP?
Exactly how do you want it to work with VB6? Do you want to be able to run that script within your VB6 application? Do you have control over the script code (ie, is this your page or is it a page from some website you have no control over)? What's with the lines in red?
-
Nov 6th, 2008, 04:07 AM
#4
Re: Convert php script to work in vb6.0
try adding a script control to your form then try evaluating the script in the script control
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 11th, 2008, 10:22 AM
#5
Thread Starter
Member
Re: Convert php script to work in vb6.0
My mistake, Is it javascript, is it possible to set the webbrowser1 html to that code with out navigating to a webpage and execute the script? I couldn't figure out the script control.
-
Nov 11th, 2008, 10:04 PM
#6
Thread Starter
Member
Re: Convert Javascript to work in vb6.0
Friendly bump, Edited my question.
-
Nov 12th, 2008, 04:30 PM
#7
Re: Convert Javascript to work in vb6.0
OK, here's how you do it.
On a VB Form add a Script control. It is in Components under Microsoft Script Control 1.0
Add two Command buttons
Add a Textbox
Copy and Past the below code to your project
Code:
Private Sub Command1_Click()
Dim InputData As String
Open App.Path & "\JavaScript.txt" For Binary As #1
InputData = String(LOF(1), 0)
Get #1, 1, InputData
Close #1
Text1.Text = InputData
End Sub
Private Sub Command2_Click()
ScriptControl1.Language = "JavaScript"
ScriptControl1.Reset
ScriptControl1.Timeout = NoTimeout
ScriptControl1.AddObject "Any text you want", Me, True
ScriptControl1.AddCode Text1.Text
ScriptControl1.Run "begin"
End Sub
Public Sub ShowAnswerInVBApplication(s As String)
'
' This sub is called by the Java script in function 'begin'
'
MsgBox s
End Sub
Now change the Java script code you have from your post #1 to the following:
Code:
function begin()
{
//
// This function was added so that the Java Script can work in
// a Visual Basic application.
var hc = 0 + parseInt(1234) % 123431 + parseInt(1234) % 123431;
//
// Call the Public Sub in the Visual Basic application passing it the answer
//
ShowAnswerInVBApplication(hex_sha1("" + hc));
}
function hex_sha1(s)
{
return binb2hex(core_sha1(str2binb(s),s.length*8));
}
function core_sha1(x, len)
{
x[len>>5]|=0x80<<(24-len%32);
x[((len+64>>9)<<4)+15]=len;
var w=Array(80);
var a=1732584193;
var b= -271733879;
var c= -1732584194;
var d=271733878;
var e= -1009589776;
for(var i=0;i<x.length;i+=16)
{
var olda=a;
var oldb=b;
var oldc=c;
var oldd=d;
var olde=e;
for(var j=0;j<80;j++)
{
if(j<16)
{
w[j]=x[i+j];
}
else
{
w[j]=((w[j-3]^w[j-8]^w[j-14]^w[j-16])<<1)|((w[j-3]^w[j-8]^w[j-14]^w[j-16])>>>(32-1));
}
var y2=(j<20)?1518500249:(j<40)?1859775393:(j<60)? -1894007588: -899497514;
var t=safe_add(safe_add((a<<5)|(a>>>(32-5)),sha1_ft(j,b,c,d)),safe_add(safe_add(e,w[j]),y2));
e=d;
d=c;
c=(b<<30)|(b>>>(32-30));
b=a;
a=t;
}
a=safe_add(a,olda);
b=safe_add(b,oldb);
c=safe_add(c,oldc);
d=safe_add(d,oldd);
e=safe_add(e,olde);
}
return Array(a,b,c,d,e);
}
function sha1_ft(t,b,c,d)
{
if(t<20)
{
return(b&c)|((~b)&d);
}
if(t<40)
{
return b^c^d;
}
if(t<60)
{
return(b&c)|(b&d)|(c&d);
}
return b^c^d;
}
function safe_add(x,y)
{
return((x>>16)+(y>>16)+((x&0xFFFF)+(y&0xFFFF)>>16)<<16)|((x&0xFFFF)+(y&0xFFFF)&0xFFFF);
}
function str2binb(str)
{
var bin=Array();
var mask=(1<<8)-1;
for(var i=0; i<str.length*8; i+=8)
{
bin[i>>5]|=(str.charCodeAt(i/8)&mask)<<(32-8-i%32);
}
return bin;
}
function binb2hex(binarray)
{
var hex_tab=0?"0123456789ABCDEF":"0123456789abcdef";
var str="";
for(var i=0; i<binarray.length*4; i++)
{
str+=hex_tab.charAt((binarray[i>>2]>>((3-i%4)*8+4))&0xF)+hex_tab.charAt((binarray[i>>2]>>((3-i%4)*8))&0xF);
}
return str;
}
Enjoy!
-
Nov 12th, 2008, 05:43 PM
#8
Re: Convert Javascript to work in vb6.0
If you want to run the script in a WebBrowser then all you need to do is to add a WebBrowser control to your Form and a Command button.
The script code as you show it in your post #1 remains unchanged but save it as a file and give it a file name like script_test.html
In the Command click event just do this
Code:
Private Sub Command1_Click()
WebBrowser1.Navigate App.Path & "\script_test.html"
End Sub
Only problem is when doing it this way you cannot get back the answer in your VB application unless you add some more code which I will show you if you are interested.
-
Nov 12th, 2008, 06:12 PM
#9
Thread Starter
Member
Re: Convert Javascript to work in vb6.0
I love you guys , Gonna try both now.. Thanks!
-
Nov 12th, 2008, 06:30 PM
#10
Thread Starter
Member
Re: Convert Javascript to work in vb6.0
 Originally Posted by jmsrickland
OK, here's how you do it.
On a VB Form add a Script control. It is in Components under Microsoft Script Control 1.0
Add two Command buttons
Add a Textbox
Copy and Past the below code to your project
Code:
Private Sub Command1_Click()
Dim InputData As String
Open App.Path & "\JavaScript.txt" For Binary As #1
InputData = String(LOF(1), 0)
Get #1, 1, InputData
Close #1
Text1.Text = InputData
End Sub
Private Sub Command2_Click()
ScriptControl1.Language = "JavaScript"
ScriptControl1.Reset
ScriptControl1.Timeout = NoTimeout
ScriptControl1.AddObject "Any text you want", Me, True
ScriptControl1.AddCode Text1.Text
ScriptControl1.Run "begin"
End Sub
Public Sub ShowAnswerInVBApplication(s As String)
'
' This sub is called by the Java script in function 'begin'
'
MsgBox s
End Sub
Now change the Java script code you have from your post #1 to the following:
Code:
function begin()
{
//
// This function was added so that the Java Script can work in
// a Visual Basic application.
var hc = 0 + parseInt(1234) % 123431 + parseInt(1234) % 123431;
//
// Call the Public Sub in the Visual Basic application passing it the answer
//
ShowAnswerInVBApplication(hex_sha1("" + hc));
}
function hex_sha1(s)
{
return binb2hex(core_sha1(str2binb(s),s.length*8));
}
function core_sha1(x, len)
{
x[len>>5]|=0x80<<(24-len%32);
x[((len+64>>9)<<4)+15]=len;
var w=Array(80);
var a=1732584193;
var b= -271733879;
var c= -1732584194;
var d=271733878;
var e= -1009589776;
for(var i=0;i<x.length;i+=16)
{
var olda=a;
var oldb=b;
var oldc=c;
var oldd=d;
var olde=e;
for(var j=0;j<80;j++)
{
if(j<16)
{
w[j]=x[i+j];
}
else
{
w[j]=((w[j-3]^w[j-8]^w[j-14]^w[j-16])<<1)|((w[j-3]^w[j-8]^w[j-14]^w[j-16])>>>(32-1));
}
var y2=(j<20)?1518500249:(j<40)?1859775393:(j<60)? -1894007588: -899497514;
var t=safe_add(safe_add((a<<5)|(a>>>(32-5)),sha1_ft(j,b,c,d)),safe_add(safe_add(e,w[j]),y2));
e=d;
d=c;
c=(b<<30)|(b>>>(32-30));
b=a;
a=t;
}
a=safe_add(a,olda);
b=safe_add(b,oldb);
c=safe_add(c,oldc);
d=safe_add(d,oldd);
e=safe_add(e,olde);
}
return Array(a,b,c,d,e);
}
function sha1_ft(t,b,c,d)
{
if(t<20)
{
return(b&c)|((~b)&d);
}
if(t<40)
{
return b^c^d;
}
if(t<60)
{
return(b&c)|(b&d)|(c&d);
}
return b^c^d;
}
function safe_add(x,y)
{
return((x>>16)+(y>>16)+((x&0xFFFF)+(y&0xFFFF)>>16)<<16)|((x&0xFFFF)+(y&0xFFFF)&0xFFFF);
}
function str2binb(str)
{
var bin=Array();
var mask=(1<<8)-1;
for(var i=0; i<str.length*8; i+=8)
{
bin[i>>5]|=(str.charCodeAt(i/8)&mask)<<(32-8-i%32);
}
return bin;
}
function binb2hex(binarray)
{
var hex_tab=0?"0123456789ABCDEF":"0123456789abcdef";
var str="";
for(var i=0; i<binarray.length*4; i++)
{
str+=hex_tab.charAt((binarray[i>>2]>>((3-i%4)*8+4))&0xF)+hex_tab.charAt((binarray[i>>2]>>((3-i%4)*8))&0xF);
}
return str;
}
Enjoy!
Thanks again? Do you have a paypal acct?
-
Nov 12th, 2008, 06:48 PM
#11
Re: Convert Javascript to work in vb6.0
 Originally Posted by VbCoder2008
Do you have a paypal acct? 
Yes I do but I am not going to tell you what it is
Seriously, no one on vbforums charges for help they give (or they shouldn't).
I help you and anyone I can if I can; just for the benefit of being able to do so. That's my reward
-
Nov 12th, 2008, 06:58 PM
#12
Re: Convert Javascript to work in vb6.0
 Originally Posted by VbCoder2008
Thanks again? Do you have a paypal acct? 
You could bump his Rating.
Just click the '(Rate)' below his name
-
Nov 12th, 2008, 09:11 PM
#13
Thread Starter
Member
Re: Convert Javascript to work in vb6.0
 Originally Posted by jmsrickland
Yes I do but I am not going to tell you what it is
Seriously, no one on vbforums charges for help they give (or they shouldn't).
I help you and anyone I can if I can; just for the benefit of being able to do so. That's my reward 
Your to modest. I searched everywhere trying to get it to work. Thanks again
-
Nov 12th, 2008, 11:15 PM
#14
Re: Convert Javascript to work in vb6.0
Not modest; it's the reason for this forum and it's members.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|