rejetto forum

Implementing Base64 encoding/decoding as a Macro function

LeoNeeson · 26 · 14291

0 Members and 1 Guest are viewing this topic.

Offline LeoNeeson

  • Tireless poster
  • ****
    • Posts: 842
  • Status: On hiatus (sporadically here)
    • View Profile
    • twitter.com/LeoNeeson
EDIT: As reference, I comment this thread was started here and then moved here. The idea started after reading the code of mobil-light_V4.2beta.zip by dj (thanks him for the inspiration, Mars for the actual code implementation that you will find below, and Rejetto, because he took this idea into consideration). Now it follows the original post...



Offtopic: Reading your template, I always wondered is a file section (in the HFS template), could store a file encoded in Base64, and that be decoded by the server on-the-fly (delivering the decoded output).

I see you have write this:

Code: [Select]
[mystyle.css]
{.mime|text/css.}
a {text-decoration: none}

...and I expected this to deliver the same output (but it doesn't):

Code: [Select]
[mystyle.css]
{.mime|text/css;base64.}
YSB7dGV4dC1kZWNvcmF0aW9uOiBub25lfQ==

I already know that is possible to directly write Base64 code, like this:

Code: [Select]
<link rel="stylesheet" type="text/css" href="data:text/css;base64,YSB7dGV4dC1kZWNvcmF0aW9uOiBub25lfQ==" />
But I was wondering if is it possible make HFS decode Base64 'on-the-fly' using a macro like: {.mime|text/css;base64.}

Also, neither of the following works:

Code: [Select]
[mystyle.css]
{.base64|YSB7dGV4dC1kZWNvcmF0aW9uOiBub25lfQ==.}

Code: [Select]
[mystyle.css]
{.base64decode|YSB7dGV4dC1kZWNvcmF0aW9uOiBub25lfQ==.}

I'm curious to know if this is possible using macros. ???
« Last Edit: November 04, 2018, 08:48:35 PM by LeoNeeson »
HFS in Spanish (HFS en Español) / How to compile HFS (Tutorial)
» Currently taking a break, until HFS v2.4 get his stable version.


Offline dj

  • Tireless poster
  • ****
    • Posts: 291
  • 👣 🐾
    • View Profile
    • PWAs
I can't test at the moment, but I don't find the scripting command base64decode in the docu.
Why do you need this?


Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2059
    • View Profile
they do not exist in hfs, but this is not the realm of the impossible


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13510
    • View Profile
@leo
i don't understand what's the point of doing something like that


Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2059
    • View Profile

But I was wondering if is it possible make HFS decode Base64 'on-the-fly' using a macro like: {.mime|text/css;base64.}

Also, neither of the following works:

Code: [Select]
[mystyle.css]
{.base64|YSB7dGV4dC1kZWNvcmF0aW9uOiBub25lfQ==.}

Code: [Select]
[mystyle.css]
{.base64decode|YSB7dGV4dC1kZWNvcmF0aW9uOiBub25lfQ==.}

I'm curious to know if this is possible using macros. ???

it would be enough simply to add this in scriptlib.pas before 'encodeuri'

Quote
    if name = 'encode64' then
      result:=base64encode(p);

    if name = 'decode64' then
      result:=base64decode(p);
      

including OverbyteicsMD5and OverbyteicsSha1 in the uses,we can allow two other codifications

 
Quote
   if name = 'encodemd5' then
      result:=strMD5(p);

    if name = 'encodesha1' then
      result:=SHA1ofStr(p);   

the latter two can be used to code the sending of a verification code or password

a wacky example as how encrypt download url and redirect it to be managed in a section

   
Quote
   {.if|{.=|%item-type%|folder.}|<a href="%item-url%">|<a href="/~download?name=%item-name%&id={.encode64|%item-full-url%.}&md5={.encodemd5|%item-url%.}">.}

[download|no log]
<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="0; url={.decode64|{.urlvar|id.}.}">
        <script type="text/javascript">
            window.location.href = "{.decode64|{.urlvar|id.}.}"
        </script>
        <title>Page Redirection</title>
    </head>
    <body>
        <!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->
        If you are not redirected automatically, follow this <a href='{.decode64|{.urlvar|id.}.}'>direct link </a>.
    </body>
</html>
   



Offline LeoNeeson

  • Tireless poster
  • ****
    • Posts: 842
  • Status: On hiatus (sporadically here)
    • View Profile
    • twitter.com/LeoNeeson
@leo
i don't understand what's the point of doing something like that
Why do you need this?
Because I'm a Base64 fan?... ;D Talking seriously, I'm surprised this was not already a Macro, since those functions (base64encode and base64decode) are part of the source code (I thought they were already accessible using a Macro). The code that Mars wrote, would be a useful addition, for many things. I can't make a list of useful cases, but having Base64 encoding/decoding at Macro level, could come handy for many things. Mars added a nice example (and also including MD5/SHA1 encoding), but there are many uses of Base64.

it would be enough simply to add this in scriptlib.pas before 'encodeuri'
Code: [Select]
    if name = 'encode64' then
      result:=base64encode(p);

    if name = 'decode64' then
      result:=base64decode(p);
That's great, and an easy addition! :)
HFS in Spanish (HFS en Español) / How to compile HFS (Tutorial)
» Currently taking a break, until HFS v2.4 get his stable version.


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13510
    • View Profile
ok, i will add these
Code: [Select]
    if name = 'base64' then
      result:=base64encode(p);
    if name = 'base64decode' then
      result:=base64decode(p);
    if name = 'md5' then
        result:=strMD5(p);

but couldn't find the sha1 available


Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2059
    • View Profile
for my use I would just add aliases  ;D

encode64=base64|$1
decode64=base64decode|$1
encodemd5=md5|$1

I was based on these two macros, encodeuri and decodeuri,  which was in the sense of continuity

do not confuse the md5 and md5 file macros that have different uses

« Last Edit: November 03, 2018, 09:24:21 PM by Mars »


Offline LeoNeeson

  • Tireless poster
  • ****
    • Posts: 842
  • Status: On hiatus (sporadically here)
    • View Profile
    • twitter.com/LeoNeeson
(Much better having a separated topic) :)

but couldn't find the sha1 available
I think you will find the required 'OverbyteIcsSha1.pas' on \Delphi\vc32 folder of the 'OverbyteIcsV7Gold.zip' file. Mars could confirm this, since he tested this.
HFS in Spanish (HFS en Español) / How to compile HFS (Tutorial)
» Currently taking a break, until HFS v2.4 get his stable version.


Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2059
    • View Profile
it seems that I'm not up to date I'm only with OverbyteIcsV6
Quote
// ============================================================================
// D5-implementation of "US Secure Hash Algorithm 1 (SHA1)" (RFC3174)
// Copyright (c) 2001, Juergen Haible.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// ============================================================================

{------------------------------------------------------------------------------
Update by F. Piette for ICS (http://www.overbyte.be)
Jan 10, 2004 Defined uint32_t as LongWord instead of LongInt
Jul 23, 2004 Revised SHA1Reset to check for nil reference to comply with RFC-3174
             Made the unit compatible with Delphi 2
May 08, 2007 Added a few casts to uint32_t to constants.
Jul 30, 2007 V2.00 Updated for .NET
Mar 24, 2008 V2.01 Made some changes to prepare code for Unicode
                   Use only AnsiString

------------------------------------------------------------------------------}


 with a portable vesion of Borland® Delphi® for Microsoft® Windows™ Version 10.0.2558.35231

all the resources I add are placed in a separate subdirectory in lib and added to the options in the form $ (BDS) \ lib \ ... ,  this allows my delphi to be transferred to any media as usb key for use  it on any machine
« Last Edit: November 03, 2018, 09:38:34 PM by Mars »


Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2059
    • View Profile
@LeoNeeson
do you have what it takes on your machine to compile hfs sources and create your executable?


Offline LeoNeeson

  • Tireless poster
  • ****
    • Posts: 842
  • Status: On hiatus (sporadically here)
    • View Profile
    • twitter.com/LeoNeeson
@LeoNeeson
do you have what it takes on your machine to compile hfs sources and create your executable?
I haven't tested yet, because I need to have all the components listed on developer notes.txt, but I'm working on it (I currently have Portable Delphi and OverByte's ICS component only). I need some time to find the rest and put everything in the correct folder.

...with a portable version of Borland® Delphi® for Microsoft® Windows™ Version 10.0.2558.35231
I recently found that version (since I'm trying to get all the components to build HFS by myself), and I think the version you are referring is: Portable Turbo Delphi Lite 1.0d.

I don't know if there is a big difference in the SHA1 implementation of OverbyteIcsV6 and OverbyteIcsV7. I only cited version 7 because I've read (in developer notes.txt), that Rejetto is using that version.
HFS in Spanish (HFS en Español) / How to compile HFS (Tutorial)
» Currently taking a break, until HFS v2.4 get his stable version.


Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2059
    • View Profile
If you lack the resources to complete your installation, I will be able to provide them but in a few days, the time to get your hands on the archives


Offline LeoNeeson

  • Tireless poster
  • ****
    • Posts: 842
  • Status: On hiatus (sporadically here)
    • View Profile
    • twitter.com/LeoNeeson
If you lack the resources to complete your installation, I will be able to provide them but in a few days, the time to get your hands on the archives
Yes, I will happy if you could share it with me :) (because I find component installation complex to me). There is no hurry, take your time. (you could send me the link by private message or directly post it here).

EDIT: To make the file more light, you could do a folder comparison between you own portable Delphi folder, and the default package of Portable Turbo Delphi Lite 1.0d. Perhaps you could use a folder diff utility like Meld or WinMerge (or any other Directory Comparison utility). Just to be sure, don't forget to make a backup of you portable Delphi before using any of those utilities.
« Last Edit: November 03, 2018, 10:30:06 PM by LeoNeeson »
HFS in Spanish (HFS en Español) / How to compile HFS (Tutorial)
» Currently taking a break, until HFS v2.4 get his stable version.


Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2059
    • View Profile
I do not bother to compare or install packages, I just have PAS files accessible in Delphi and functional.