0

I found this code in this stack overflow answer and I'm trying to understand how this recursive function ever terminates. My reasoning for not asking the question on that thread is that my question is about recursion, not so much about what was discussed there (encryption).

/// <summary>
/// Simple Encryption (AES) then Authentication (HMAC) for a UTF8 Message.
/// </summary>
/// <param name="secretMessage">The secret message.</param>
/// <param name="cryptKey">The crypt key.</param>
/// <param name="authKey">The auth key.</param>
/// <param name="nonSecretPayload">(Optional) Non-Secret Payload.</param>
/// <returns>
/// Encrypted Message
/// </returns>
/// <exception cref="System.ArgumentException">Secret Message Required!;secretMessage</exception>
/// <remarks>
/// Adds overhead of (Optional-Payload + BlockSize(16) + Message-Padded-To-Blocksize +  HMac-Tag(32)) * 1.33 Base64
/// </remarks>
public static string SimpleEncrypt(string secretMessage, byte[] cryptKey, byte[] authKey,
                   byte[] nonSecretPayload = null)
{
  if (string.IsNullOrEmpty(secretMessage))
    throw new ArgumentException("Secret Message Required!", "secretMessage");

  var plainText = Encoding.UTF8.GetBytes(secretMessage);
  //My question: There's a recursive call here but no base case specified
  var cipherText = SimpleEncrypt(plainText, cryptKey, authKey, nonSecretPayload);
  return Convert.ToBase64String(cipherText);
}
daniekpo
  • 157
  • 3
  • 9
  • 2
    Isn't plainText a byte array, so the call to SimpleEncrypt is an overload - not recursive. – PaulF Aug 07 '18 at 14:37

1 Answers1

8

It's not recursive.

var plainText = Encoding.UTF8.GetBytes() returns a byte array, causing the SimpleEncrypt(byte[] ...) overload further in the file to be called.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272