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