shill: Implement a rot47 crypto module.

This module is required for parity with flimflam and will be used to scramble
values in the perstistent store.

BUG=chromium-os:16964
TEST=unit test

Change-Id: I20de52eebfe6f9f5ccd4ddcb4c7e3e94edbb7af7
Reviewed-on: http://gerrit.chromium.org/gerrit/3258
Tested-by: Darin Petkov <petkov@chromium.org>
Reviewed-by: Darin Petkov <petkov@chromium.org>
diff --git a/crypto_rot47.cc b/crypto_rot47.cc
new file mode 100644
index 0000000..cf70fa3
--- /dev/null
+++ b/crypto_rot47.cc
@@ -0,0 +1,40 @@
+// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "shill/crypto_rot47.h"
+
+using std::string;
+
+namespace shill {
+
+const char CryptoROT47::kID[] = "rot47";
+
+string CryptoROT47::GetID() {
+  return kID;
+}
+
+bool CryptoROT47::Encrypt(const string &plaintext, string *ciphertext) {
+  const int kRotSize = 94;
+  const int kRotHalf = kRotSize / 2;
+  const char kRotMin = '!';
+  const char kRotMax = kRotMin + kRotSize - 1;
+
+  *ciphertext = plaintext;
+  for (string::iterator ch = ciphertext->begin();
+       ch != ciphertext->end(); ++ch) {
+    if (*ch < kRotMin || *ch > kRotMax) {
+      continue;
+    }
+    int rot = *ch + kRotHalf;
+    *ch = (rot > kRotMax) ? rot - kRotSize : rot;
+  }
+  return true;
+}
+
+bool CryptoROT47::Decrypt(const string &ciphertext, string *plaintext) {
+  // ROT47 is self-reciprocal.
+  return Encrypt(ciphertext, plaintext);
+}
+
+}  // namespace shill