Just starting out? Need help? Post your questions and find answers here.
Stefan Schnell
 
Posts: 46
Joined: Tue Dec 01, 2015 8:17 am

SetRequestHeader and Cipher Base64 in SpiderBasic

by Stefan Schnell Tue Dec 01, 2015 8:32 am

Hello community,

I want to transfer an authorization string via HTTP to an application server. Is it possible to add this string via SpiderBasic to a HttpRequest?

It is necessary to encode this Basic authorization with Base64. Is it possible with SpiderBasic to do that?

Is it planned to implement a cipher library in Spider Basic?

Thanks in advance for hints and answers.

Cheers
Stefan
Fred
Site Admin
 
Posts: 1132
Joined: Mon Feb 24, 2014 10:51 am

Re: SetRequestHeader and Cipher Base64 in SpiderBasic

by Fred Tue Dec 01, 2015 2:39 pm

Yes, a cipher library is planned but won't be for the next version. That said, Base64 encoding is relatively trivial, so it should be doable in SpiderBasic itself :)
e2robot
 
Posts: 38
Joined: Wed Mar 19, 2014 8:34 am

Re: SetRequestHeader and Cipher Base64 in SpiderBasic

by e2robot Tue Dec 01, 2015 11:20 pm

Here is a rough snippet using CryptoJS

There are easier ways to do Base64 but this library does heaps of other types.


Regards,
Phil


Code: Select all
!$.getScript("http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core-min.js", loadok1);
!function loadok1()
!{
Debug "OK1"
!}


!$.getScript("http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-utf16-min.js", loadok2);
!function loadok2()
!{
Debug "OK2"
!}



!$.getScript("http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js", loadok3);
!function loadok3()
!{
Debug "OK3"
!}

Procedure button1pressed()
  text.s="Hello World"
  Debug text
 !var v_words  = CryptoJS.enc.Utf8.parse(v_text);
 !var v_base = CryptoJS.enc.Base64.stringify(v_words);
   
  Debug "base " +base
EndProcedure



  If OpenWindow(0, 10,10,500,300 , "TEST")
        ButtonGadget(1 ,10, 80, 100, 100, "1")
 EndIf
 
  BindGadgetEvent(1,@button1pressed(),#PB_EventType_LeftClick)
Stefan Schnell
 
Posts: 46
Joined: Tue Dec 01, 2015 8:17 am

Re: SetRequestHeader and Cipher Base64 in SpiderBasic

by Stefan Schnell Wed Dec 02, 2015 8:58 am

Hello Fred and Phil,

thanks you for your requests, I will try it.

Cheers
Stefan
kpeters58
 
Posts: 10
Joined: Fri Dec 07, 2018 7:27 pm

Re: SetRequestHeader and Cipher Base64 in SpiderBasic

by kpeters58 Wed Dec 19, 2018 12:47 am

For those who prefer Spiderbasic code over yet another JS library:

Code: Select all

EnableExplicit
; -------------------------------------------------------------------------------------------------
Procedure.s Base64Encode(Text.s)   
  Protected base64chars.s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", i.i,
            encodedstr.s  = "", paddingstr.s = "", three_byte_number.i, n1.i, n2.i, n3.i, n4.i,
            padcount = Len(Text) % 3
 
  ; right pad to make the length of the incoming string a multiple of 3
  If (padcount > 0)
    paddingstr = Left("==", 3 - padcount)
    Text + Left(Chr(0) + Chr(0), 3 - padcount)
  EndIf 
  ; iterate over the length of the incoming string, three characters at a time
  For i = 1 To Len(Text) Step 3
    ; these three 8-bit (ASCII) characters become one 24-bit number
    three_byte_number = Asc(Mid(Text, i, 1)) << 16 + Asc(Mid(Text, i + 1, 1)) << 8 + Asc(Mid(Text, i + 2, 1))
    ; this 24-bit number then gets split into four 6-bit numbers
    n1 = 1 + (three_byte_number >> 18) & 63
    n2 = 1 + (three_byte_number >> 12) & 63
    n3 = 1 + (three_byte_number >> 6)  & 63
    n4 = 1 + (three_byte_number)       & 63
    ; those four 6-bit numbers are used as indices into the base64 character list
    encodedstr + Mid(base64chars, n1, 1) + Mid(base64chars, n2, 1) + Mid(base64chars, n3, 1) + Mid(base64chars, n4, 1)
    ; need newlines after every 76 output characters, according to MIME RFC 2045
    If ((i * 4) / 3) % 76 = 0
      encodedstr + Chr(13) + Chr(10)
    EndIf 
  Next
   ProcedureReturn Mid(encodedstr, 0, Len(encodedstr) - Len(paddingstr)) + paddingstr ; RFC 2045: trailing CR/LF ?
EndProcedure

Procedure.s BasicAuth(Username.s, Password.s)
  ProcedureReturn "Basic " + Base64Encode(Username + ":" + Password)
EndProcedure 

; Test cases - verified against base64encode.org results
Debug Base64Encode("Show me the cow")             ; should return 'U2hvdyBtZSB0aGUgY293'
Debug Base64Encode("Send money & cigarettes")     ; should return 'U2VuZCBtb25leSAmIGNpZ2FyZXR0ZXM='
Debug BasicAuth("test", "password")               ; should return 'dGVzdDpwYXNzd29yZA==''
Last edited by kpeters58 on Thu Dec 20, 2018 8:32 pm, edited 1 time in total.
User avatar
Peter
 
Posts: 579
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1

Re: SetRequestHeader and Cipher Base64 in SpiderBasic

by Peter Wed Dec 19, 2018 10:01 am

kpeters58 wrote:For those who prefer Spiderbasic code over yet another JS library:


you can also use the inbuild btoa() (and atob()) - JavaScript - functions:

Code: Select all
; https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

Procedure.s Base64EncodeString(Input.s)
  !  return btoa(encodeURIComponent(v_input).replace(/%([0-9A-F]{2})/g,
  !    function toSolidBytes(match, p1) {
  !      return String.fromCharCode('0x' + p1);
  !  }));
EndProcedure

Procedure.s Base64DecodeString(Input.s)
  !  return decodeURIComponent(atob(v_input).split('').map(function(c) {
  !    return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
  !  }).join(''));
EndProcedure

Debug Base64EncodeString("Show me the cow")             ; should return 'U2hvdyBtZSB0aGUgY293'
Debug Base64EncodeString("Send money & cigarettes")     ; should return 'U2VuZCBtb25leSAmIGNpZ2FyZXR0ZXM='
Return to Coding Questions

Who is online

Users browsing this forum: No registered users and 3 guests