Community discussions

MikroTik App
 
NHosseinzadeh
just joined
Topic Author
Posts: 2
Joined: Fri Mar 03, 2023 6:41 pm

Base64 and SHA256 function for Scripting

Fri Mar 03, 2023 6:47 pm

Hi there. I'm working on a script which sends API request to a Huawei LTE modem. to accomplish this API request i should be able to create my http post data parameters, which i should convert session id and password and... to Base64 and SHA256. is there any function or command available in ROS scripting language to do the hashing calculation for me 'offline'? sadly my MIPSBE router doesn't support container so container is not an option for me. if there is no such function available in scripting i really hope it gets implemented soon.
Best regards
Navid
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Base64 and SHA256 function for Scripting

Sat Mar 04, 2023 3:34 am

SHA 256 is too complex for me to do on spare time.

But for start on Base64, from wiki Man = TWFu

Just as start, miss the code for move the cursor by 3 elements adding necessary "=" padding.

working concept code

{
:local charsString ""
:for x from=0 to=15 step=1 do={ :for y from=0 to=15 step=1 do={
    :local tmpHex "$[:pick "0123456789ABCDEF" $x ($x+1)]$[:pick "0123456789ABCDEF" $y ($y+1)]"
    :set $charsString "$charsString$[[:parse "(\"\\$tmpHex\")"]]"
} }

:local chr2int do={:if (($1="") or ([:len $1] > 1) or ([:typeof $1] = "nothing")) do={:return -1}; :return [:find $2 $1 -1]}

# RFC 4648 base64 Standard
:local arrb64 [:toarray "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,+,/"]

# RFC 4648 base64url URL- and filename-safe standard
:local arrb64url [:toarray "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,-,_"]

:local input "Man"

:local v1 [$chr2int [:pick $input 0 1] $charsString]
:local v2 [$chr2int [:pick $input 1 2] $charsString]
:local v3 [$chr2int [:pick $input 2 3] $charsString]
:local f6bit   ($v1 >> 2)
:local s6bit ((($v1 &  3) * 16) + ($v2 >> 4))
:local t6bit ((($v2 & 15) *  4) + ($v3 >> 6))
:local q6bit   ($v3 & 63)

:put "$input = $($arrb64->$f6bit)$($arrb64->$s6bit)$($arrb64->$t6bit)$($arrb64->$q6bit) on Base64 Standard"
}
Last edited by rextended on Fri Mar 10, 2023 4:10 pm, edited 3 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Base64 and SHA256 function for Scripting

Wed Mar 08, 2023 1:55 pm

search tag # rextended str2base64 string or data to base64 encoding RFC 4648

Function to convert a string (or a Byte stream) to Base64 based on RFC 4648
Optional "url" parameter use URL and filename-safe standard encoding.
Optional "nopad" parameter do not put the padding "=" character (is optional on RFC 4648).
:global str2base64 do={
    :local input   [:tostr "$1"]
    :local options "$2$3"

    :local charsString ""
    :for x from=0 to=15 step=1 do={ :for y from=0 to=15 step=1 do={
        :local tmpHex "$[:pick "0123456789ABCDEF" $x ($x+1)]$[:pick "0123456789ABCDEF" $y ($y+1)]"
        :set $charsString "$charsString$[[:parse "(\"\\$tmpHex\")"]]"
    } }

    :local chr2int do={:if (($1="") or ([:len $1] > 1) or ([:typeof $1] = "nothing")) do={:return -1}; :return [:find $2 $1 -1]}

    # RFC 4648 base64 Standard
    :local arrb64 [:toarray "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z\
                            ,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z\
                            ,0,1,2,3,4,5,6,7,8,9,+,/,="]
    :if ($options~"url") do={
        # RFC 4648 base64url URL and filename-safe standard
        :set arrb64 [:toarray "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z\
                              ,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z\
                              ,0,1,2,3,4,5,6,7,8,9,-,_,="]
    }
    :if ($options~"nopad") do={:set ($arrb64->64) ""}

    :local position 0
    :local output   "" ; :local work ""
    :local v1 "" ; :local v2 "" ; :local v3 "" ; :local f6bit 0 ; :local s6bit 0 ; :local t6bit 0 ; :local q6bit 0
    :while ($position < [:len $input]) do={
        :set work [:pick $input $position ($position + 3)]
        :set v1 [$chr2int [:pick $work 0 1] $charsString]
        :set v2 [$chr2int [:pick $work 1 2] $charsString]
        :set v3 [$chr2int [:pick $work 2 3] $charsString]
        :set f6bit   ($v1 >> 2)
        :set s6bit ((($v1 &  3) * 16) + ($v2 >> 4))
        :set t6bit ((($v2 & 15) *  4) + ($v3 >> 6))
        :set q6bit   ($v3 & 63)
        :if ([:len $work] < 2) do={ :set t6bit 64}
        :if ([:len $work] < 3) do={ :set q6bit 64}
        :set output   "$output$($arrb64->$f6bit)$($arrb64->$s6bit)$($arrb64->$t6bit)$($arrb64->$q6bit)"
        :set position ($position + 3)
    }
    :return $output
}

example code

[] > :put [$str2base64 "ManManMan"]
TWFuTWFuTWFu

[] > :put [$str2base64 "ManManMa"]
TWFuTWFuTWE=

[] > :put [$str2base64 "ManManM"]
TWFuTWFuTQ==

[] > :put [$str2base64 "Hi from MikroTik"]
SGkgZnJvbSBNaWtyb1Rpaw==

[] > :put [$str2base64 "Hi from MikroTik" "nopad"]
SGkgZnJvbSBNaWtyb1Rpaw

[] > :put [$str2base64 ">>>>"]
Pj4+Pg==

[] > :put [$str2base64 ">>>>" "url"]
Pj4-Pg==

[] > :put [$str2base64 ">>>>" "url" "nopad"]
Pj4-Pg
Last edited by rextended on Fri Mar 10, 2023 4:12 pm, edited 3 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Base64 and SHA256 function for Scripting

Thu Mar 09, 2023 12:22 pm

search tag # rextended decode base64 to string or data based on RFC 4648

Function for decode a Base64 string based on RFC 4648

Optional "url" parameter use URL and filename-safe encoding instead ot the standard encoding.

Optional "mustpad" parameter, if is present, consider invalid Base64 input when do not have the required padding (is optional on RFC 4648).

Optional "ignoreotherchr" parameter: if is present one non-ecoding character, continue the decoding sequence skipping that.
For RFC 4648 instead is a blocking error.

:global base64dec do={
    :local input   [:tostr "$1"]
    :local options "$2$3$4"

    :local charsString ""
    :for x from=0 to=15 step=1 do={ :for y from=0 to=15 step=1 do={
        :local tmpHex "$[:pick "0123456789ABCDEF" $x ($x+1)]$[:pick "0123456789ABCDEF" $y ($y+1)]"
        :set $charsString "$charsString$[[:parse "(\"\\$tmpHex\")"]]"
    } }

    # RFC 4648 base64 Standard
    :local arrb64 [:toarray "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z\
                            ,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z\
                            ,0,1,2,3,4,5,6,7,8,9,+,/,="]
    :if ($options~"url") do={
        # RFC 4648 base64url URL and filename-safe standard
        :set arrb64 [:toarray "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z\
                              ,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z\
                              ,0,1,2,3,4,5,6,7,8,9,-,_,="]
    }

    :if ($options~"mustpad") do={
        :if (([:len $input] % 4) != 0) do={:error "Invalid length, must be padded with one or more ="}
    }

    :if ($options~"ignoreotherchr") do={
        :local position 0
        :local tmpchar   ""
        :local tmpstring ""
        :while ($position < [:len $input]) do={
            :set tmpchar [:pick $input $position ($position + 1)]
            :if ([:typeof [:find $arrb64 $tmpchar]] != "nil") do={:set tmpstring "$tmpstring$tmpchar"}
            :set position ($position + 1)
        }
        :set input $tmpstring
    }

    :local position 0
    :local output   "" ; :local work ""
    :local v1 0 ; :local v2 0 ; :local v3 0 ; :local v4 0 ; :local fchr "" ; :local schr "" ; :local tchr ""
    :while ($position < [:len $input]) do={
        :set work [:pick $input $position ($position + 4)]
        :set v1 [:find $arrb64 [:pick $work 0 1]]
        :set v2 [:find $arrb64 [:pick $work 1 2]]
        :set v3 [:find $arrb64 [:pick $work 2 3]]
        :set v4 [:find $arrb64 [:pick $work 3 4]]
        :if (([:typeof $v1] = "nil") or ([:typeof $v2] = "nil") or ([:typeof $v3] = "nil") or ([:typeof $v4] = "nil")) do={
            :error "Unexpected character, invalid Base64 sequence"
        }
        :if ([:typeof [:pick $work 1 2]] = "nil") do={
            :if ($options~"ignoreotherchr") do={:set v2 64 ; :set v3 64 ; :set v4 64} else={:error "Required 2nd character is missing"}
        }
        :if (([:typeof [:pick $work 2 3]] = "nil") and (($v2 & 15) != 0)) do={
            :if ($options~"ignoreotherchr") do={:set v3 64 ; :set v4 64} else={:error "Required 3rd character is missing"}
        }
        :if (([:typeof [:pick $work 3 4]] = "nil") and (($v3 &  3) != 0)) do={
            :if ($options~"ignoreotherchr") do={:set v4 64} else={:error "Required 4th character is missing"}
        }
        :set fchr [:pick $charsString  (($v1 << 2)       + ($v2 >> 4))]
        :set schr [:pick $charsString ((($v2 & 15) << 4) + ($v3 >> 2))]
        :set tchr [:pick $charsString ((($v3 &  3) << 6) +  $v4     ) ]
        :if ($v4 = 64) do={:set tchr "" ; :set position [:len $input]}
        :if ($v3 = 64) do={:set schr "" ; :set position [:len $input]}
        :if ($v2 = 64) do={:set fchr "" ;
                               :if ($options~"ignoreotherchr") do={
                                   :set position [:len $input]
                               } else={
                                   :error "Unexpected padding character ="
                               }
                          }
        :set output   "$output$fchr$schr$tchr"
        :set position ($position + 4)
    }
    :return $output
}

examples code

[] > :put [$base64dec "SGkgZnJvbSBNaWtyb1Rpaw"]
Hi from MikroTik

[] > :put [$base64dec "SGkgZnJvbSBNaWtyb1Rpaw" "mustpad"]
>>>(error on terminal)<<<
Invalid length, must be padded with one or more =

[] > :put [$base64dec "SGkgZnJvbSBNaWtyb1Rpaw==" "mustpad"]
Hi from MikroTik


# note: this string is invalid, w is replaced with = on purpose
[] > :put [$base64dec "SGkgZnJvbSBNaWtyb1Rpa="]
>>>(error on terminal)<<<
Unexpected padding character =

# note: this string is invalid, w is replaced with = on purpose
[] > :put [$base64dec "SGkgZnJvbSBNaWtyb1Rpa=" "ignoreotherchr"]
Hi from MikroTi


# the error are the 3 "!"
[] > :put [$base64dec "SGkgZnJvbSBN!!!aWtyb1Rpaw=="]
Unexpected character, invalid Base64 sequence

[] > :put [$base64dec "SGkgZnJvbSBN!!!aWtyb1Rpaw==" "ignoreotherchr"]
Hi from MikroTik
Last edited by rextended on Fri Mar 10, 2023 8:07 pm, edited 3 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Base64 and SHA256 function for Scripting

Fri Mar 10, 2023 2:18 pm

search tag # rextended str2base16 string or data to base16 encoding

Function to convert a string (or a Byte stream) to Base16

Optional "lowercase" use lowercase a-f in place of the correct A-F

:global str2base16 do={
    :local input   [:tostr "$1"]
    :local options [:tostr "$2"]

    :local charsString ""
    :for x from=0 to=15 step=1 do={ :for y from=0 to=15 step=1 do={
        :local tmpHex "$[:pick "0123456789ABCDEF" $x ($x+1)]$[:pick "0123456789ABCDEF" $y ($y+1)]"
        :set $charsString "$charsString$[[:parse "(\"\\$tmpHex\")"]]"
    } }

    :local hexchars "0123456789ABCDEF"
    :if ($options~"lowercase") do={
        :set hexchars "0123456789abcdef"
    }
    :local chr2hex do={
        :local input [:find $2 $1 -1]
        :local convert [:pick $3 (($input >> 4) & 0xF)]
        :set convert ($convert.[:pick $3 ($input & 0xF)])
        :return $convert
    }

    :local position 0
    :local output   "" ; :local work ""
    :while ($position < [:len $input]) do={
        :set work [$chr2hex [:pick $input $position ($position + 1)] $charsString $hexchars]
        :set output   "$output$work"
        :set position ($position + 1)
    }
    :return $output
}

example code

[] > :put [$str2base16 "Hi from MikroTik"]
48692066726F6D204D696B726F54696B
Last edited by rextended on Fri Mar 10, 2023 9:52 pm, edited 4 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Base64 and SHA256 function for Scripting

Fri Mar 10, 2023 2:37 pm

search tag # rextended base16dec base16 to string or data decoding

Function to convert Base16 to a string (or a Byte stream)

Note: the function accept mixed A-F a-f case.

Option "ignoreodd" ignore if the decoded string have odd value (must be have only character pairs).

pratically is a revised function old hexstr2chrstr:
viewtopic.php?f=9&t=129693&p=871742#p871742

:global base16dec do={
    :local input   [:tostr "$1"]
    :local options [:tostr "$2"]

    :local hex2chr do={:return [[:parse "(\"\\$1\")"]]}
    :local lowerarray {"a"="A";"b"="B";"c"="C";"d"="D";"e"="E";"f"="F"}

    :if (!($input~"^[0-9A-Fa-f]*\$")) do={
        :error "invalid characters: only 0-9, A-F and a-f are valid Base16 values"
    }

    :if (!($options~"ignoreodd")) do={
        :if (([:len $input] % 2) != 0) do={:error "Invalid length, is odd."}
    }

    :local position 0
    :local output   "" ; :local work "" ; :local chk1 "" ; :local chk2 ""
    :while ($position < [:len $input]) do={
        :set chk1 [:pick $input $position       ($position + 1)]
        :set chk2 [:pick $input ($position + 1) ($position + 2)]
        :if ($chk1~"[a-f]") do={:set chk1 ($lowerarray->$chk1)}
        :if ($chk2~"[a-f]") do={:set chk2 ($lowerarray->$chk2)}
        :set work "$chk1$chk2"
        :if ([:len $work] = 2) do={
            :set work [$hex2chr $work]
        } else={
            :set work ""
        }
        :set output   "$output$work"
        :set position ($position + 2)
    }
    :return $output
}

example code

[] > :put [$base16dec "48692066726F6D204D696B726F54696B"]
Hi from MikroTik

[] > :put [$base16dec "48692066726F6D204D696B726F54696" "ignoreodd"]
Hi from MikroTi
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Base64 and SHA256 function for Scripting

Fri Mar 10, 2023 7:39 pm

Why not also the Base32??? :lol:

search tag # rextended str2base32 string or data to base32 encoding RFC 4648

Function to convert a string (or a Byte stream) to Base32 based on RFC 4648
Optional "nopad" parameter do not put the padding "=" character (is optional on RFC 4648).
Optional "hex" parameter use RFC 4648 base32 Extended Hex Alphabet encoding instead ot the standard encoding.

:global str2base32 do={
    :local input   [:tostr "$1"]
    :local options "$2$3"

    :local charsString ""
    :for x from=0 to=15 step=1 do={ :for y from=0 to=15 step=1 do={
        :local tmpHex "$[:pick "0123456789ABCDEF" $x ($x+1)]$[:pick "0123456789ABCDEF" $y ($y+1)]"
        :set $charsString "$charsString$[[:parse "(\"\\$tmpHex\")"]]"
    } }

    :local chr2int do={:if (($1="") or ([:len $1] > 1) or ([:typeof $1] = "nothing")) do={:return -1}; :return [:find $2 $1 -1]}

    # RFC 4648 base32 Standard
    :local arrb32 [:toarray "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,2,3,4,5,6,7,="]
    :if ($options~"hex") do={
        # RFC 4648 base32 Extended Hex Alphabet
        :set arrb32 [:toarray "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,="]
    }
    :if ($options~"nopad") do={:set ($arrb32->32) ""}

    :local position 0
    :local output   "" ; :local work ""
    :local v1 "" ; :local v2 "" ; :local v3 "" ; :local v4 "" ; :local v5 ""
    :local fir5bit 0 ; :local sec5bit 0 ; :local thi5bit 0 ; :local qua5bit 0
    :local fif5bit 0 ; :local six5bit 0 ; :local sep5bit 0 ; :local eig5bit 0
    :while ($position < [:len $input]) do={
        :set work [:pick $input $position ($position + 5)]
        :set v1 [$chr2int [:pick $work 0 1] $charsString]
        :set v2 [$chr2int [:pick $work 1 2] $charsString]
        :set v3 [$chr2int [:pick $work 2 3] $charsString]
        :set v4 [$chr2int [:pick $work 3 4] $charsString]
        :set v5 [$chr2int [:pick $work 4 5] $charsString]

        :set fir5bit   ($v1 >> 3)
        :set sec5bit ((($v1 &  7) *  4) + ($v2 >> 6))
        :set thi5bit  (($v2 >> 1) & 31)
        :set qua5bit ((($v1 &  1) * 16) + ($v3 >> 4))
        :set fif5bit ((($v3 & 15) *  2) + ($v4 >> 7))
        :set six5bit  (($v4 >> 2) & 31)
        :set sep5bit ((($v4 &  3) *  8) + ($v5 >> 5))
        :set eig5bit   ($v5 & 31)

        :if ([:len $work] < 2) do={:set thi5bit 32 ; :set qua5bit 32}
        :if ([:len $work] < 3) do={:set fif5bit 32                 }
        :if ([:len $work] < 4) do={:set six5bit 32 ; :set sep5bit 32}
        :if ([:len $work] < 5) do={:set eig5bit 32                 }

        :set output   "$output$($arrb32->$fir5bit)$($arrb32->$sec5bit)$($arrb32->$thi5bit)$($arrb32->$qua5bit)"
        :set output   "$output$($arrb32->$fif5bit)$($arrb32->$six5bit)$($arrb32->$sep5bit)$($arrb32->$eig5bit)"
        :set position ($position + 5)
    }
    :return $output
}

example code

[] > :put [$str2base32 "ManaM"]
JVQW4YKN

[] > :put [$str2base32 "Man"]
JVQW4===

[] > :put [$str2base32 "Man" "nopad"]
JVQW4

[] > :put [$str2base32 "ManaM" "hex"]
9LGMSOAD

[] > :put [$str2base32 "Man" "hex"]
9LGMS===

[] > :put [$str2base32 "Man" "hex" "nopad"]
9LGMS
Last edited by rextended on Fri Mar 10, 2023 9:51 pm, edited 5 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Base64 and SHA256 function for Scripting

Fri Mar 10, 2023 9:36 pm

search tag # rextended decode base32 to string or data based on RFC 4648

Function for decode a Base32 string based on RFC 4648

Optional "mustpad" parameter, if is present, consider invalid Base32 input when do not have the required padding (is optional on RFC 4648).
Optional "hex" parameter use RFC 4648 base32 Extended Hex Alphabet encoding instead ot the standard encoding.

:global base32dec do={
    :local input   [:tostr "$1"]
    :local options "$2$3"

    :local charsString ""
    :for x from=0 to=15 step=1 do={ :for y from=0 to=15 step=1 do={
        :local tmpHex "$[:pick "0123456789ABCDEF" $x ($x+1)]$[:pick "0123456789ABCDEF" $y ($y+1)]"
        :set $charsString "$charsString$[[:parse "(\"\\$tmpHex\")"]]"
    } }

    # RFC 4648 base32 Standard
    :local arrb32 [:toarray "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,2,3,4,5,6,7,="]
    :if ($options~"hex") do={
        # RFC 4648 base32 Extended Hex Alphabet
        :set arrb32 [:toarray "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,="]
    }

    :if ($options~"mustpad") do={
        :if (([:len $input] % 8) != 0) do={:error "Invalid length, must be padded with one or more ="}
    }

    :local position 0
    :local output   "" ; :local work ""
    :local v1 0 ; :local v2 0 ; :local v3 0 ; :local v4 0 ; :local v5 0 ; :local v6 0 ; :local v7 0 ; :local v8 0
    :local firchr "" ; :local secchr "" ; :local thichr "" ; :local quachr "" ; :local fifchr ""
    :while ($position < [:len $input]) do={
        :set work [:pick $input $position ($position + 8)]
        :set v1 [:find $arrb32 [:pick $work 0 1]]
        :set v2 [:find $arrb32 [:pick $work 1 2]]
        :set v3 [:find $arrb32 [:pick $work 2 3]]
        :set v4 [:find $arrb32 [:pick $work 3 4]]
        :set v5 [:find $arrb32 [:pick $work 4 5]]
        :set v6 [:find $arrb32 [:pick $work 5 6]]
        :set v7 [:find $arrb32 [:pick $work 6 7]]
        :set v8 [:find $arrb32 [:pick $work 7 8]]
        :if (([:typeof $v1] = "nil") or ([:typeof $v2] = "nil") or ([:typeof $v3] = "nil") or ([:typeof $v4] = "nil") or \
             ([:typeof $v5] = "nil") or ([:typeof $v6] = "nil") or ([:typeof $v7] = "nil") or ([:typeof $v8] = "nil")) do={
            :error "Unexpected character, invalid Base32 sequence"
        }

        :if (([:typeof [:pick $work 1 2]] = "nil") and ([:pick $work 0 1] != "=")) \
            do={:error "Required 2nd character is missing"}
        :if (([:typeof [:pick $work 2 3]] = "nil") and (($v2 & 3) != 0)) \
            do={:error "Required 3rd character is missing"}
        :if (([:typeof [:pick $work 3 4]] = "nil") and ((($v2 & 3) != 0) or ([:typeof [:pick $work 2 3]] != "nil"))) \
            do={:error "Required 4th character is missing"}
        :if (([:typeof [:pick $work 4 5]] = "nil") and (($v4 & 15) != 0)) \
            do={:error "Required 5th character is missing"}
        :if (([:typeof [:pick $work 5 6]] = "nil") and (($v5 & 1) != 0)) \
            do={:error "Required 6th character is missing"}
        :if (([:typeof [:pick $work 6 7]] = "nil") and ((($v5 & 1) != 0) or ([:typeof [:pick $work 5 6]] != "nil"))) \
            do={:error "Required 7th character is missing"}
        :if (([:typeof [:pick $work 7 8]] = "nil") and (($v7 & 7) != 0)) \
            do={:error "Required 8th character is missing"}

        :set firchr [:pick $charsString (( $v1       *   8)             + ($v2 >> 2))]
        :set secchr [:pick $charsString ((($v2 &  3) *  64) + ($v3 * 2) + ($v4 >> 4))]
        :set thichr [:pick $charsString ((($v4 & 15) *  16)             + ($v5 >> 1))]
        :set quachr [:pick $charsString ((($v5 &  1) * 128) + ($v6 * 4) + ($v7 >> 3))]
        :set fifchr [:pick $charsString ((($v7 &  7) *  32)             +  $v8      )]

        :if ($v1 != 32) do={
            :if (  $v2 = 32                                       ) do={:error "Unexpected padding character ="}
            :if ((($v3 = 32)  or ($v4 = 32)) and (($v2 &  3) != 0)) do={:error "Unexpected padding character ="}
            :if ( ($v4 = 32) and ($v3!= 32)                       ) do={:error "Unexpected padding character ="}
            :if (( $v5 = 32                ) and (($v4 & 15) != 0)) do={:error "Unexpected padding character ="}
            :if ((($v6 = 32)  or ($v7 = 32)) and (($v5 &  1) != 0)) do={:error "Unexpected padding character ="}
            :if ( ($v7 = 32) and ($v6!= 32)                       ) do={:error "Unexpected padding character ="}
            :if ( ($v8 = 32)                 and (($v7 &  7) != 0)) do={:error "Unexpected padding character ="}
        }

        :if ($v8 = 32) do={:set fifchr "" ; :set position [:len $input]}
        :if ($v7 = 32) do={:set quachr "" ; :set position [:len $input]}
        :if ($v6 = 32) do={:set quachr "" ; :set position [:len $input]}
        :if ($v5 = 32) do={:set thichr "" ; :set position [:len $input]}
        :if ($v4 = 32) do={:set thichr "" ; :set position [:len $input]}
        :if ($v3 = 32) do={:set secchr "" ; :set position [:len $input]}
        :if ($v2 = 32) do={:set firchr "" ; :set position [:len $input]}
        :if ($v1 = 32) do={:set firchr "" ; :set position [:len $input]}

        :set output   "$output$firchr$secchr$thichr$quachr$fifchr"
        :set position ($position + 8)
    }
    :return $output
}

example code

[] > :put [$base32dec "JVQW4YKN"]
ManaM

[] > :put [$base32dec "9LGMSOAD" "hex"]
ManaM

Autodetect between standard or hex is not possible, because
standard have X,Y,Z and not 0,1,8,9
hex have 0,1,8,9 but not W,X,Y,Z
but that not suffice for distinguish correctly the code, because one valid code can also contain only non distinguishable characters.
 
User avatar
noyo
Member Candidate
Member Candidate
Posts: 116
Joined: Sat Jan 28, 2012 12:25 am
Location: Mazury - Poland
Contact:

Re: Base64 and SHA256 function for Scripting

Tue Aug 01, 2023 10:54 pm

@NHosseinzadeh You did it?
@rextended sha256 hash converter is necessary
 
nathan1
Member Candidate
Member Candidate
Posts: 160
Joined: Sat Jan 16, 2016 7:05 pm

Re: Base64 and SHA256 function for Scripting

Wed Nov 01, 2023 7:15 pm

search tag # rextended str2base64 string or data to base64 encoding RFC 4648
....
Hi @rextended,

I wanted to make you aware that I used your str2base64 in my ha-mikrotik project code: https://github.com/svlsResearch/ha-mikr ... ver.script

It was very useful for handling some character oddities to make it all working with v7. Please let me know if you have any issues with this use, I credited you in the code.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Base64 and SHA256 function for Scripting

Fri Nov 03, 2023 2:07 am

It was very useful for handling some character oddities to make it all working with v7. Please let me know if you have any issues with this use, I credited you in the code.
No problem, in fact I thank you for writing it. ;)

Who is online

Users browsing this forum: No registered users and 21 guests