[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / services / base64 / base64.service.js
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 /**\r
21  * Author: Rui Lu\r
22  * 12/15/2016\r
23  */\r
24 (function () {\r
25     class Base64Service {\r
26          constructor(){\r
27                 \r
28          }\r
29          encode(input) {\r
30                  var keyStr = 'ABCDEFGHIJKLMNOP' +\r
31              'QRSTUVWXYZabcdef' +\r
32              'ghijklmnopqrstuv' +\r
33              'wxyz0123456789+/' +\r
34              '=';\r
35                  var output = "";\r
36              var chr1, chr2, chr3 = "";\r
37              var enc1, enc2, enc3, enc4 = "";\r
38              var i = 0;\r
39 \r
40              do {\r
41                  chr1 = input.charCodeAt(i++);\r
42                  chr2 = input.charCodeAt(i++);\r
43                  chr3 = input.charCodeAt(i++);\r
44 \r
45                  enc1 = chr1 >> 2;\r
46                  enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);\r
47                  enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);\r
48                  enc4 = chr3 & 63;\r
49 \r
50                  if (isNaN(chr2)) {\r
51                      enc3 = enc4 = 64;\r
52                  } else if (isNaN(chr3)) {\r
53                      enc4 = 64;\r
54                  }\r
55 \r
56                  output = output +\r
57                          keyStr.charAt(enc1) +\r
58                          keyStr.charAt(enc2) +\r
59                          keyStr.charAt(enc3) +\r
60                          keyStr.charAt(enc4);\r
61                  chr1 = chr2 = chr3 = "";\r
62                  enc1 = enc2 = enc3 = enc4 = "";\r
63              } while (i < input.length);\r
64 \r
65              return output;\r
66          }\r
67     }\r
68     angular.module('ecompApp').service('base64Service', Base64Service)\r
69 })();\r