O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Our Native Code

For our native code, we’re going to create an implementation of the Caesar cipher.[27] This isn’t something you would use for real encryption, but if you think about the implications of bugs in native crypto implementations, the impact is pretty huge.

Let’s start with our legacy code:

 caesar.c
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
 
 char *caesar(int shift, char *input)
 {
  char *output = malloc(strlen(input));
  memset(output, '\0', strlen(input));
 
  for (int x = 0; x < strlen(input); x++) {
  if (isalpha(input[x])) {
  int c = toupper(input[x]);
  c = (((c - 65) + shift) % 26) + 65;
  output[x] = c;
  } else {
  output[x] = input[x];
  }
  }
 
  return output;
 }

Like all substitution ciphers, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required