]> git.corax.cc Git - toolbox/commitdiff
include/is: Add is_base64() function
authorMatthias Kruk <matthias.kruk@miraclelinux.com>
Fri, 18 Jun 2021 07:25:08 +0000 (16:25 +0900)
committerMatthias Kruk <matthias.kruk@miraclelinux.com>
Fri, 18 Jun 2021 07:33:58 +0000 (16:33 +0900)
This commit adds is_base64(), a convenience function that allows the
caller to check if a string is base64 encoded.

include/is.sh

index d2c39c55a2e482dc620f96a07195d8b0ef255bc1..ea8453a7d39626c70fd6700bdabaf299307e5824 100644 (file)
@@ -1,4 +1,4 @@
-#!bin/bash
+#!/bin/bash
 
 # is.sh - ctype-style functions for Toolbox
 # Copyright (C) 2021 Matthias Kruk
@@ -44,6 +44,23 @@ is_hex() {
        return 1
 }
 
+is_base64() {
+       local str="$1"
+
+       # The length must be a multiple of 4 and there
+       # must not be more than two bytes of padding.
+
+       if (( ${#str} % 4 != 0 )); then
+               return 1
+       fi
+
+       if [[ "$str" =~ ^[a-zA-Z0-9+/]+[=]{,2}$ ]]; then
+               return 0
+       fi
+
+       return 1
+}
+
 is_upper() {
        local str