blob: cf70fa30bbe35d48158c9012f577f93ed6bcc2a7 [file] [log] [blame]
Darin Petkov877642b2011-06-27 13:37:22 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/crypto_rot47.h"
6
7using std::string;
8
9namespace shill {
10
11const char CryptoROT47::kID[] = "rot47";
12
13string CryptoROT47::GetID() {
14 return kID;
15}
16
17bool CryptoROT47::Encrypt(const string &plaintext, string *ciphertext) {
18 const int kRotSize = 94;
19 const int kRotHalf = kRotSize / 2;
20 const char kRotMin = '!';
21 const char kRotMax = kRotMin + kRotSize - 1;
22
23 *ciphertext = plaintext;
24 for (string::iterator ch = ciphertext->begin();
25 ch != ciphertext->end(); ++ch) {
26 if (*ch < kRotMin || *ch > kRotMax) {
27 continue;
28 }
29 int rot = *ch + kRotHalf;
30 *ch = (rot > kRotMax) ? rot - kRotSize : rot;
31 }
32 return true;
33}
34
35bool CryptoROT47::Decrypt(const string &ciphertext, string *plaintext) {
36 // ROT47 is self-reciprocal.
37 return Encrypt(ciphertext, plaintext);
38}
39
40} // namespace shill