blob: 79c427d911cc9db5ff31c53b3d983146ab1ce8dc [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
Darin Petkov86964e02011-06-29 13:49:28 -070013CryptoROT47::CryptoROT47() {}
14
Darin Petkov877642b2011-06-27 13:37:22 -070015string CryptoROT47::GetID() {
16 return kID;
17}
18
19bool CryptoROT47::Encrypt(const string &plaintext, string *ciphertext) {
20 const int kRotSize = 94;
21 const int kRotHalf = kRotSize / 2;
22 const char kRotMin = '!';
23 const char kRotMax = kRotMin + kRotSize - 1;
24
25 *ciphertext = plaintext;
26 for (string::iterator ch = ciphertext->begin();
27 ch != ciphertext->end(); ++ch) {
28 if (*ch < kRotMin || *ch > kRotMax) {
29 continue;
30 }
31 int rot = *ch + kRotHalf;
32 *ch = (rot > kRotMax) ? rot - kRotSize : rot;
33 }
34 return true;
35}
36
37bool CryptoROT47::Decrypt(const string &ciphertext, string *plaintext) {
38 // ROT47 is self-reciprocal.
39 return Encrypt(ciphertext, plaintext);
40}
41
42} // namespace shill