blob: a934355d00377e33767e0801da1357c7ba978693 [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;
Paul Stewart6db7b242014-05-02 15:34:21 -070026 for (auto &ch : *ciphertext) {
27 if (ch < kRotMin || ch > kRotMax) {
Darin Petkov877642b2011-06-27 13:37:22 -070028 continue;
29 }
Paul Stewart6db7b242014-05-02 15:34:21 -070030 int rot = ch + kRotHalf;
31 ch = (rot > kRotMax) ? rot - kRotSize : rot;
Darin Petkov877642b2011-06-27 13:37:22 -070032 }
33 return true;
34}
35
36bool CryptoROT47::Decrypt(const string &ciphertext, string *plaintext) {
37 // ROT47 is self-reciprocal.
38 return Encrypt(ciphertext, plaintext);
39}
40
41} // namespace shill