NetTalk Central

Author Topic: Translate to clarion  (Read 5396 times)

willie53

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Translate to clarion
« on: October 01, 2015, 11:38:39 AM »
I'm trying put all my code within a clarion app and I have a small c# dll I use sign a url for google maps, and I want to get rid of this dll.

Has anyone or can anyone translate the code below to clarion, I'm figuring I can use some of the functions in kryptonite.


      public string GetGoogleURLSignRequest(string url, string ClientID, string APIKey)
        {
            SignedUrl = string.Empty;
           ASCIIEncoding encoding = new ASCIIEncoding();

            // converting key to bytes will throw an exception, need to replace '-' and '_' characters first
            string usablePrivateKey = APIKey.Replace("-", "+").Replace("_", "/");
            byte[] privateKeyBytes = Convert.FromBase64String(usablePrivateKey);

            Uri uri = new Uri(url);
            byte[] encodedPathAndQueryBytes = encoding.GetBytes(uri.LocalPath + uri.Query);

            //Compute the hash
            HMACSHA1 algorithm = new HMACSHA1(privateKeyBytes);
            byte[] hash = algorithm.ComputeHash(encodedPathAndQueryBytes);

            // convert the bytes to string and make url-safe by replacing '+' and '/' characters
            string signature = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_");

            // Add the signature to the existing URI.
            SignedUrl = uri.Scheme + "://" + uri.Host + uri.LocalPath + uri.Query + "&signature=" + signature;

            return SignedUrl;

MyBrainIsFull

  • Full Member
  • ***
  • Posts: 134
    • View Profile
Re: Translate to clarion
« Reply #1 on: October 05, 2015, 04:56:40 PM »
Hi Willie, looks do-able - drop me a line - NetTalkCentral@3d.com.au,  I have a question for you.

Kevin

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11247
    • View Profile
Re: Translate to clarion
« Reply #2 on: October 08, 2015, 07:31:43 AM »
Is this still unresolved?

willie53

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Translate to clarion
« Reply #3 on: October 08, 2015, 08:25:38 AM »
I replied to kevin earlier today.. but the one line that's confusing both of us is this one..

   byte[] encodedPathAndQueryBytes = encoding.GetBytes(uri.LocalPath + uri.Query);

 it's seem that it trying convert to bytes in an array the uRL path and URL query.
the path is something like this /maps/api/geocode/json
the query is something like this /?address=13 main st&channel=dev&sensor=false&client=gme-clientid

image attached


[attachment deleted by admin]
« Last Edit: October 09, 2015, 05:30:01 AM by willie53 »

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11247
    • View Profile
Re: Translate to clarion
« Reply #4 on: October 13, 2015, 12:24:37 AM »
I think it's just a string.
After all "strings" are just "strings of bytes".

I would just treat it as a string and go from there...

cheers
Bruce

MyBrainIsFull

  • Full Member
  • ***
  • Posts: 134
    • View Profile
Re: Translate to clarion
« Reply #5 on: October 13, 2015, 01:44:28 PM »
Yes Bruce, we did,  its a string.

UPDATE
I had to tweak it a bit, but it WORKS!!!!   
Thanks so much with helping with this.     

GetGoogleURLSignRequest ( GoogleURL, PathUrl, Query , ClientID, APIKey)
stPKey  StringTheory
stQry     StringTheory

            stPKey.setValue(ApiKey)
            stPKey.Replace('-', '+')
            stPKey.Replace('_', '/')
            stPKey.Base64Decode( )        ! st object holds the privateKey
!!------------------------------------------------!
            stQry.SetValue(PathUrl)
            stQry.Base64Decode( )        ! st object holds the path info
            stQry.Append( Query )   
            Loc:SecretKey = stpkey.getvalue()
            err#  = Crypto.MakeHmac (stQry, Loc:SecretKey, cs:Calg_Sha1)       
  ! returns Crypto:Ok or Crypto:NotOk   

            stQry.Replace('+', '-')
            stQry.Replace('/', '_')
            stQry.Prepend(Clip(GoogleURL) & Clip(PathUrl) & Clip(Query) & '&Signature=' ) !& stQry.GetValue() )

            return stQry.GetValue( )