From 85c9b5c00f9c010cfc94907878c85585080af15d Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Fri, 18 Jun 2021 16:25:08 +0900 Subject: [PATCH] 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. --- include/is.sh | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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 -- 2.47.3