Determines whether the specified payload is base64. - CSharp System

CSharp examples for System:String Base64

Description

Determines whether the specified payload is base64.

Demo Code

/*//w w  w. j  a v  a2 s. c  o m
*  Warewolf - The Easy Service Bus
*  Copyright 2014 by Warewolf Ltd <alpha@warewolf.io>
*  Licensed under GNU Affero General Public License 3.0 or later. 
*  Some rights reserved.
*  Visit our website for more information <http://warewolf.io/>
*  AUTHORS <http://warewolf.io/authors.php> , CONTRIBUTORS <http://warewolf.io/contributors.php>
*  @license GNU Affero General Public License <http://www.gnu.org/licenses/agpl-3.0.html>
*/
using System.Xml;
using System.Text.RegularExpressions;
using System.Linq;
using System.Globalization;
using System;

public class Main{
        /// <summary>
        /// Determines whether the specified payload is base64.
        /// </summary>
        /// <param name="payload">The payload.</param>
        /// <returns>
        ///   <c>true</c> if the specified payload is base64; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsBase64(this string payload)
        {
            bool result = false;
            try
            {
                // ReSharper disable ReturnValueOfPureMethodIsNotUsed
                Convert.FromBase64String(payload);
                // ReSharper restore ReturnValueOfPureMethodIsNotUsed
                result = true;
            }
            // ReSharper disable EmptyGeneralCatchClause
            catch(Exception)
            // ReSharper restore EmptyGeneralCatchClause
            {
                // if error is thrown we know it is not a valid base64 string
            }

            return result;
        }
}

Related Tutorials