Completed
Push — master ( 74a408...4c6568 )
by Dongxin
10s
created

blog-encrypt.js ➔ decryptAES   B

Complexity

Conditions 5
Paths 17

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 5
c 2
b 0
f 1
nc 17
nop 0
dl 0
loc 32
rs 8.439
1
'use strict';
2
3
function decryptAES() {
4
  var pass = String(document.getElementById("pass").value);
5
  try {
6
    var content = CryptoJS.AES.decrypt(document.getElementById("encrypt-blog").innerHTML.trim(), pass);
0 ignored issues
show
Bug introduced by
The variable CryptoJS seems to be never declared. If this is a global, consider adding a /** global: CryptoJS */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
7
    content = content.toString(CryptoJS.enc.Utf8);
8
    content = decodeBase64(content);
9
    content = unescape(content);
10
    if (content === "") {
11
      throw new Error("内容为空"); // ???
12
    } else {
13
      document.getElementById("encrypt-blog").style.display = "inline";
14
      document.getElementById("encrypt-blog").innerHTML = "";
15
      // use jquery to load some js code
16
      $("#encrypt-blog").html(content);
17
        document.getElementById("security").style.display  = "none";
18
        if (document.getElementById("toc-div")) {
19
          document.getElementById("toc-div").style.display = "inline";
20
        }
21
      }
22
      // Call MathJax to render
23
      if(typeof MathJax !== "undefined") {
0 ignored issues
show
Bug introduced by
The variable MathJax seems to be never declared. If this is a global, consider adding a /** global: MathJax */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
24
        MathJax.Hub.Queue(
25
          ['resetEquationNumbers', MathJax.InputJax.TeX],
26
          ['PreProcess', MathJax.Hub],
27
          ['Reprocess', MathJax.Hub]
28
        );
29
      }
30
    } catch (e) {
31
      alert("解密失败");
32
      console.log(e);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
33
    }
34
}
35
36
function htmlDecode (str) {
37
  var s = "";
38
  if (str.length == 0) return "";
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
39
40
  s = str.replace(/>/g, "&");
41
  s = s.replace(/&lt;/g,   "<");
42
  s = s.replace(/&gt;/g,   ">");
43
  s = s.replace(/&nbsp;/g, "    "); // ??? why not ' '
44
  s = s.replace(/'/g,      "\'");
45
  s = s.replace(/&quot;/g, "\"");
46
  s = s.replace(/<br>/g,   "\n");
47
  return s;
48
}
49
50
function decodeBase64(content) {
51
  content = CryptoJS.enc.Base64.parse(content);
0 ignored issues
show
Bug introduced by
The variable CryptoJS seems to be never declared. If this is a global, consider adding a /** global: CryptoJS */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
52
  content = CryptoJS.enc.Utf8.stringify(content);
53
  return content;
54
}
55
56
// Since you decided to use jQuery.
57
$(document).ready(
58
  function(){
59
    console.log("Registering Enter for decrypt.");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
60
    document.getElementById("pass").onkeypress = function(keyPressEvent) {
61
     if (keyPressEvent.keyCode === 13) {
62
       decryptAES();
63
     }
64
    }
65
  }
66
);
67