<commit_before>﻿using System;
using System.ComponentModel.DataAnnotations;

namespace Bit.Api.Utilities
{
    /// <summary>
    /// Validates a string that is in encrypted form: "b64iv=|b64ct="
    /// </summary>
    public class EncryptedStringAttribute : ValidationAttribute
    {
        public EncryptedStringAttribute()
            : base("{0} is not a valid encrypted string.")
        { }

        public override bool IsValid(object value)
        {
            if(value == null)
            {
                return true;
            }

            try
            {
                var encString = value?.ToString();
                if(string.IsNullOrWhiteSpace(encString))
                {
                    return false;
                }

                var encStringPieces = encString.Split('|');
                if(encStringPieces.Length != 2)
                {
                    return false;
                }

                var iv = Convert.FromBase64String(encStringPieces[0]);
                var ct = Convert.FromBase64String(encStringPieces[1]);

                if(iv.Length < 1 || ct.Length < 1)
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }

            return true;
        }
    }
}<commit_msg>Allow mac for EncryptedString<commit_after>﻿using System;
using System.ComponentModel.DataAnnotations;

namespace Bit.Api.Utilities
{
    /// <summary>
    /// Validates a string that is in encrypted form: "b64iv=|b64ct="
    /// </summary>
    public class EncryptedStringAttribute : ValidationAttribute
    {
        public EncryptedStringAttribute()
            : base("{0} is not a valid encrypted string.")
        { }

        public override bool IsValid(object value)
        {
            if(value == null)
            {
                return true;
            }

            try
            {
                var encString = value?.ToString();
                if(string.IsNullOrWhiteSpace(encString))
                {
                    return false;
                }

                var encStringPieces = encString.Split('|');
                if(encStringPieces.Length != 2 && encStringPieces.Length != 3)
                {
                    return false;
                }

                var iv = Convert.FromBase64String(encStringPieces[0]);
                var ct = Convert.FromBase64String(encStringPieces[1]);

                if(iv.Length < 1 || ct.Length < 1)
                {
                    return false;
                }

                if(encStringPieces.Length == 3)
                {
                    var mac = Convert.FromBase64String(encStringPieces[2]);
                    if(mac.Length < 1)
                    {
                        return false;
                    }
                }
            }
            catch
            {
                return false;
            }

            return true;
        }
    }
}
{'fraction_non_alphanumeric': 0.08756218905472637, 'fraction_numerical': 0.011276948590381426, 'mean_word_length': 1.0958999305072967, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6029140', 'n_tokens_mistral': 802, 'n_tokens_neox': 721, 'n_words': 154}