private static bool TryGetNanoAlgorithm(ICMAC referent, int macSize, out ICMAC algorithm) { if (macSize == -1) { algorithm = referent; return(true); } else if (macSize == referent.BlockSize) { algorithm = referent; return(true); } else if (macSize >= 8 && macSize <= referent.BlockSize && macSize % 8 == 0) { algorithm = new CMAC(((CMAC)referent).BlockAlgorithm, macSize); return(true); } else { algorithm = null; return(false); } }
/// <summary> /// Try get algorithm from mechanism. /// <para/>Legal algorithm's block size 64 or 128 bits. /// <para/>Legal mac size is between 8 and block size (8 bits increments). /// <para/>Used block size as mac size by default. /// </summary> /// <param name="mechanism">Algorithm mechanism.</param> /// <param name="macSize">MAC size bits.</param> /// <param name="algorithm">Algorithm.</param> /// <returns></returns> public static bool TryGetAlgorithm(string mechanism, int macSize, out ICMAC algorithm) { mechanism = mechanism.Replace('_', '-').ToUpperInvariant(); if (mechanism.EndsWith("CMAC")) { if (mechanism.EndsWith("/CMAC") || mechanism.EndsWith("-CMAC")) { mechanism = mechanism.Substring(0, mechanism.Length - 5); } else { mechanism = mechanism.Substring(0, mechanism.Length - 4); } } mechanism = mechanism.Replace('/', '-'); switch (mechanism) { case "AES": return(TryGetNanoAlgorithm(AES_CMAC, macSize, out algorithm)); case "BLOWFISH": return(TryGetNanoAlgorithm(Blowfish_CMAC, macSize, out algorithm)); case "CAMELLIA": return(TryGetNanoAlgorithm(Camellia_CMAC, macSize, out algorithm)); case "CAST5": return(TryGetNanoAlgorithm(CAST5_CMAC, macSize, out algorithm)); case "CAST6": return(TryGetNanoAlgorithm(CAST6_CMAC, macSize, out algorithm)); case "DES": return(TryGetNanoAlgorithm(DES_CMAC, macSize, out algorithm)); case "DESEDE": case "DESEDE3": case "TDEA": case "TRIPLEDES": case "3DES": return(TryGetNanoAlgorithm(DESede_CMAC, macSize, out algorithm)); case "DSTU7624-128": return(TryGetNanoAlgorithm(DSTU7624_128_CMAC, macSize, out algorithm)); case "GOST28147": return(TryGetNanoAlgorithm(GOST28147_CMAC, macSize, out algorithm)); case "IDEA": return(TryGetNanoAlgorithm(IDEA_CMAC, macSize, out algorithm)); case "NOEKEON": return(TryGetNanoAlgorithm(Noekeon_CMAC, macSize, out algorithm)); case "RC2": return(TryGetNanoAlgorithm(RC2_CMAC, macSize, out algorithm)); case "RC5": case "RC5-32": return(TryGetNanoAlgorithm(RC5_CMAC, macSize, out algorithm)); case "RC5-64": return(TryGetNanoAlgorithm(RC5_64_CMAC, macSize, out algorithm)); case "RC6": return(TryGetNanoAlgorithm(RC6_CMAC, macSize, out algorithm)); case "RIJNDAEL-128": case "RIJNDAEL128": return(TryGetNanoAlgorithm(Rijndael_128_CMAC, macSize, out algorithm)); case "SEED": return(TryGetNanoAlgorithm(SEED_CMAC, macSize, out algorithm)); case "SERPENT": return(TryGetNanoAlgorithm(Serpent_CMAC, macSize, out algorithm)); case "SKIPJACK": return(TryGetNanoAlgorithm(SKIPJACK_CMAC, macSize, out algorithm)); case "SM4": return(TryGetNanoAlgorithm(SM4_CMAC, macSize, out algorithm)); case "TEA": return(TryGetNanoAlgorithm(TEA_CMAC, macSize, out algorithm)); case "TNEPRES": return(TryGetNanoAlgorithm(Tnepres_CMAC, macSize, out algorithm)); case "TWOFISH": return(TryGetNanoAlgorithm(Twofish_CMAC, macSize, out algorithm)); case "XTEA": return(TryGetNanoAlgorithm(XTEA_CMAC, macSize, out algorithm)); default: algorithm = null; return(false); } }
/// <summary> /// Try get algorithm from mechanism. /// <para/>Legal algorithm's block size 64 or 128 bits. /// <para/>Legal mac size is between 8 and block size (8 bits increments). /// <para/>Used block size as mac size by default. /// </summary> /// <param name="mechanism">Algorithm mechanism.</param> /// <param name="algorithm">Algorithm.</param> /// <returns></returns> public static bool TryGetAlgorithm(string mechanism, out ICMAC algorithm) { return(TryGetAlgorithm(mechanism, -1, out algorithm)); }