Darin Petkov | 877642b | 2011-06-27 13:37:22 -0700 | [diff] [blame] | 1 | // 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 | |
| 7 | using std::string; |
| 8 | |
| 9 | namespace shill { |
| 10 | |
| 11 | const char CryptoROT47::kID[] = "rot47"; |
| 12 | |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 13 | CryptoROT47::CryptoROT47() {} |
| 14 | |
Darin Petkov | 877642b | 2011-06-27 13:37:22 -0700 | [diff] [blame] | 15 | string CryptoROT47::GetID() { |
| 16 | return kID; |
| 17 | } |
| 18 | |
| 19 | bool 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 | |
| 37 | bool CryptoROT47::Decrypt(const string &ciphertext, string *plaintext) { |
| 38 | // ROT47 is self-reciprocal. |
| 39 | return Encrypt(ciphertext, plaintext); |
| 40 | } |
| 41 | |
| 42 | } // namespace shill |