PageRenderTime 23ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/mercurial/src/org/netbeans/modules/mercurial/config/Scrambler.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 338 lines | 262 code | 11 blank | 65 comment | 6 complexity | 7c57fc9cbeecb69c17b26e6efbef82d8 MD5 | raw file
  1. /**
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is the CVS Client Library.
  30. * The Initial Developer of the Original Software is Robert Greig.
  31. * Portions created by Robert Greig are Copyright (C) 2000.
  32. * All Rights Reserved.
  33. *
  34. * If you wish your version of this file to be governed by only the CDDL
  35. * or only the GPL Version 2, indicate your decision by adding
  36. * "[Contributor] elects to include this software in this distribution
  37. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  38. * single choice of license, a recipient has the option to distribute
  39. * your version of this file under either the CDDL, the GPL Version 2 or
  40. * to extend the choice of license to its licensees as provided above.
  41. * However, if you add GPL Version 2 code and therefore, elected the GPL
  42. * Version 2 license, then the option applies only if the new code is
  43. * made subject to such option by the copyright holder.
  44. *
  45. * Contributor(s): Robert Greig.
  46. */
  47. package org.netbeans.modules.mercurial.config;
  48. import java.io.IOException;
  49. import org.netbeans.modules.proxy.Base64Encoder;
  50. /**
  51. * Scrambles text (the password) using the standard scheme described in the
  52. * CVS protocol version 1.10. This encoding is trivial and should not be
  53. * used for security, but rather as a mechanism for avoiding inadvertant
  54. * compromise.
  55. * @author Robert Greig, Tomas Stupka
  56. */
  57. public class Scrambler {
  58. /**
  59. * The mapping array
  60. */
  61. private int[] shifts;
  62. /**
  63. * The single instance of this class (Singleton design pattern)
  64. */
  65. public static Scrambler instance;
  66. /**
  67. * Do not instantiate the scrambler directly. Use the getInstance() method
  68. */
  69. private Scrambler() {
  70. int i;
  71. shifts = new int[256];
  72. for (i = 0; i < 32; ++i) {
  73. shifts[i] = i;
  74. }
  75. shifts[i++] = 114;
  76. shifts[i++] = 120;
  77. shifts[i++] = 53;
  78. shifts[i++] = 79;
  79. shifts[i++] = 96;
  80. shifts[i++] = 109;
  81. shifts[i++] = 72;
  82. shifts[i++] = 108;
  83. shifts[i++] = 70;
  84. shifts[i++] = 64;
  85. shifts[i++] = 76;
  86. shifts[i++] = 67;
  87. shifts[i++] = 116;
  88. shifts[i++] = 74;
  89. shifts[i++] = 68;
  90. shifts[i++] = 87;
  91. shifts[i++] = 111;
  92. shifts[i++] = 52;
  93. shifts[i++] = 75;
  94. shifts[i++] = 119;
  95. shifts[i++] = 49;
  96. shifts[i++] = 34;
  97. shifts[i++] = 82;
  98. shifts[i++] = 81;
  99. shifts[i++] = 95;
  100. shifts[i++] = 65;
  101. shifts[i++] = 112;
  102. shifts[i++] = 86;
  103. shifts[i++] = 118;
  104. shifts[i++] = 110;
  105. shifts[i++] = 122;
  106. shifts[i++] = 105;
  107. shifts[i++] = 41;
  108. shifts[i++] = 57;
  109. shifts[i++] = 83;
  110. shifts[i++] = 43;
  111. shifts[i++] = 46;
  112. shifts[i++] = 102;
  113. shifts[i++] = 40;
  114. shifts[i++] = 89;
  115. shifts[i++] = 38;
  116. shifts[i++] = 103;
  117. shifts[i++] = 45;
  118. shifts[i++] = 50;
  119. shifts[i++] = 42;
  120. shifts[i++] = 123;
  121. shifts[i++] = 91;
  122. shifts[i++] = 35;
  123. shifts[i++] = 125;
  124. shifts[i++] = 55;
  125. shifts[i++] = 54;
  126. shifts[i++] = 66;
  127. shifts[i++] = 124;
  128. shifts[i++] = 126;
  129. shifts[i++] = 59;
  130. shifts[i++] = 47;
  131. shifts[i++] = 92;
  132. shifts[i++] = 71;
  133. shifts[i++] = 115;
  134. shifts[i++] = 78;
  135. shifts[i++] = 88;
  136. shifts[i++] = 107;
  137. shifts[i++] = 106;
  138. shifts[i++] = 56;
  139. shifts[i++] = 36;
  140. shifts[i++] = 121;
  141. shifts[i++] = 117;
  142. shifts[i++] = 104;
  143. shifts[i++] = 101;
  144. shifts[i++] = 100;
  145. shifts[i++] = 69;
  146. shifts[i++] = 73;
  147. shifts[i++] = 99;
  148. shifts[i++] = 63;
  149. shifts[i++] = 94;
  150. shifts[i++] = 93;
  151. shifts[i++] = 39;
  152. shifts[i++] = 37;
  153. shifts[i++] = 61;
  154. shifts[i++] = 48;
  155. shifts[i++] = 58;
  156. shifts[i++] = 113;
  157. shifts[i++] = 32;
  158. shifts[i++] = 90;
  159. shifts[i++] = 44;
  160. shifts[i++] = 98;
  161. shifts[i++] = 60;
  162. shifts[i++] = 51;
  163. shifts[i++] = 33;
  164. shifts[i++] = 97;
  165. shifts[i++] = 62;
  166. shifts[i++] = 77;
  167. shifts[i++] = 84;
  168. shifts[i++] = 80;
  169. shifts[i++] = 85;
  170. shifts[i++] = 223;
  171. shifts[i++] = 225;
  172. shifts[i++] = 216;
  173. shifts[i++] = 187;
  174. shifts[i++] = 166;
  175. shifts[i++] = 229;
  176. shifts[i++] = 189;
  177. shifts[i++] = 222;
  178. shifts[i++] = 188;
  179. shifts[i++] = 141;
  180. shifts[i++] = 249;
  181. shifts[i++] = 148;
  182. shifts[i++] = 200;
  183. shifts[i++] = 184;
  184. shifts[i++] = 136;
  185. shifts[i++] = 248;
  186. shifts[i++] = 190;
  187. shifts[i++] = 199;
  188. shifts[i++] = 170;
  189. shifts[i++] = 181;
  190. shifts[i++] = 204;
  191. shifts[i++] = 138;
  192. shifts[i++] = 232;
  193. shifts[i++] = 218;
  194. shifts[i++] = 183;
  195. shifts[i++] = 255;
  196. shifts[i++] = 234;
  197. shifts[i++] = 220;
  198. shifts[i++] = 247;
  199. shifts[i++] = 213;
  200. shifts[i++] = 203;
  201. shifts[i++] = 226;
  202. shifts[i++] = 193;
  203. shifts[i++] = 174;
  204. shifts[i++] = 172;
  205. shifts[i++] = 228;
  206. shifts[i++] = 252;
  207. shifts[i++] = 217;
  208. shifts[i++] = 201;
  209. shifts[i++] = 131;
  210. shifts[i++] = 230;
  211. shifts[i++] = 197;
  212. shifts[i++] = 211;
  213. shifts[i++] = 145;
  214. shifts[i++] = 238;
  215. shifts[i++] = 161;
  216. shifts[i++] = 179;
  217. shifts[i++] = 160;
  218. shifts[i++] = 212;
  219. shifts[i++] = 207;
  220. shifts[i++] = 221;
  221. shifts[i++] = 254;
  222. shifts[i++] = 173;
  223. shifts[i++] = 202;
  224. shifts[i++] = 146;
  225. shifts[i++] = 224;
  226. shifts[i++] = 151;
  227. shifts[i++] = 140;
  228. shifts[i++] = 196;
  229. shifts[i++] = 205;
  230. shifts[i++] = 130;
  231. shifts[i++] = 135;
  232. shifts[i++] = 133;
  233. shifts[i++] = 143;
  234. shifts[i++] = 246;
  235. shifts[i++] = 192;
  236. shifts[i++] = 159;
  237. shifts[i++] = 244;
  238. shifts[i++] = 239;
  239. shifts[i++] = 185;
  240. shifts[i++] = 168;
  241. shifts[i++] = 215;
  242. shifts[i++] = 144;
  243. shifts[i++] = 139;
  244. shifts[i++] = 165;
  245. shifts[i++] = 180;
  246. shifts[i++] = 157;
  247. shifts[i++] = 147;
  248. shifts[i++] = 186;
  249. shifts[i++] = 214;
  250. shifts[i++] = 176;
  251. shifts[i++] = 227;
  252. shifts[i++] = 231;
  253. shifts[i++] = 219;
  254. shifts[i++] = 169;
  255. shifts[i++] = 175;
  256. shifts[i++] = 156;
  257. shifts[i++] = 206;
  258. shifts[i++] = 198;
  259. shifts[i++] = 129;
  260. shifts[i++] = 164;
  261. shifts[i++] = 150;
  262. shifts[i++] = 210;
  263. shifts[i++] = 154;
  264. shifts[i++] = 177;
  265. shifts[i++] = 134;
  266. shifts[i++] = 127;
  267. shifts[i++] = 182;
  268. shifts[i++] = 128;
  269. shifts[i++] = 158;
  270. shifts[i++] = 208;
  271. shifts[i++] = 162;
  272. shifts[i++] = 132;
  273. shifts[i++] = 167;
  274. shifts[i++] = 209;
  275. shifts[i++] = 149;
  276. shifts[i++] = 241;
  277. shifts[i++] = 153;
  278. shifts[i++] = 251;
  279. shifts[i++] = 237;
  280. shifts[i++] = 236;
  281. shifts[i++] = 171;
  282. shifts[i++] = 195;
  283. shifts[i++] = 243;
  284. shifts[i++] = 233;
  285. shifts[i++] = 253;
  286. shifts[i++] = 240;
  287. shifts[i++] = 194;
  288. shifts[i++] = 250;
  289. shifts[i++] = 191;
  290. shifts[i++] = 155;
  291. shifts[i++] = 142;
  292. shifts[i++] = 137;
  293. shifts[i++] = 245;
  294. shifts[i++] = 235;
  295. shifts[i++] = 163;
  296. shifts[i++] = 242;
  297. shifts[i++] = 178;
  298. shifts[i++] = 152;
  299. }
  300. /**
  301. * Get an instance of the standard scrambler
  302. */
  303. public static Scrambler getInstance() {
  304. if (instance == null) {
  305. instance = new Scrambler();
  306. }
  307. return instance;
  308. }
  309. public String descramble(String scrambledText) {
  310. StringBuffer buf = new StringBuffer();
  311. if (scrambledText != null) {
  312. byte[] decoded = decode(scrambledText);
  313. for (int i = 1; i < decoded.length; ++i) {
  314. buf.append(scramble((char)decoded[i]));
  315. }
  316. }
  317. return buf.toString();
  318. }
  319. private char scramble(char ch) {
  320. int i = ( shifts[ (((int) ch) & 0xFF) & 255] & 255 ) &
  321. ( shifts[ (((int) ch) & 0x00FF) & 255] & 255 );
  322. return Character.toChars(i)[0];
  323. }
  324. private byte[] decode(String str) {
  325. return Base64Encoder.decode(str);
  326. }
  327. }