Android Code Snippet Check if a string is Base64 (isBase64)

Discussion in 'Code Snippets' started by Douglas Farias, Oct 19, 2018.

  1. Douglas Farias

    Douglas Farias Expert Licensed User

    Hi all.
    I found this example in some old folders on my pc and decided to share here.
    it is a simple code to check if a string is base64 hash.

    This example use a regex.

    Code:
    Sub isBase64(text As StringAs Boolean
        
    If Regex.IsMatch("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$", text.Trim) And text.Length > 3  Then
            
    Return True
        
    Else
            
    Return False
        
    End If
    End Sub

    Example of use
    Code:
    ' TESTA A STRING CODIFICADA COM BASE64
        Log(isBase64("ZG91Z2xhc2Zhcmlhcw==")) '=douglasfarias
     
        
    ' TESTA A STRING COM TEXTO
        Log(isBase64("Test string"))'=dont is a base64 string

    The result log is
    true
    false

    Credits: stackoverflow, Douglas Farias and Flavio Rocha
     

    Attached Files:

    Last edited: Dec 2, 2018
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice