From: Matthias Kruk Date: Fri, 18 Jun 2021 07:25:08 +0000 (+0900) Subject: include/is: Add is_base64() function X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=85c9b5c00f9c010cfc94907878c85585080af15d;p=toolbox include/is: Add is_base64() function This commit adds is_base64(), a convenience function that allows the caller to check if a string is base64 encoded. --- diff --git a/include/is.sh b/include/is.sh index d2c39c5..ea8453a 100644 --- a/include/is.sh +++ b/include/is.sh @@ -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