浏览代码

Merge branch 'artemisbot-bug/uuid'

n1474335 7 年之前
父节点
当前提交
8c29ce95e2
共有 2 个文件被更改,包括 15 次插入19 次删除
  1. 1 0
      src/core/config/modules/Default.js
  2. 14 19
      src/core/operations/UUID.js

+ 1 - 0
src/core/config/modules/Default.js

@@ -39,6 +39,7 @@ import UUID from "../../operations/UUID.js";
  *  - Utils.js
  *  - Utils.js
  *    - CryptoJS
  *    - CryptoJS
  *  - otp
  *  - otp
+ *  - crypto
  *
  *
  * @author n1474335 [n1474335@gmail.com]
  * @author n1474335 [n1474335@gmail.com]
  * @copyright Crown Copyright 2017
  * @copyright Crown Copyright 2017

+ 14 - 19
src/core/operations/UUID.js

@@ -1,3 +1,6 @@
+import crypto from "crypto";
+
+
 /**
 /**
  * UUID operations.
  * UUID operations.
  *
  *
@@ -17,25 +20,17 @@ const UUID = {
      * @returns {string}
      * @returns {string}
      */
      */
     runGenerateV4: function(input, args) {
     runGenerateV4: function(input, args) {
-        if (window && typeof(window.crypto) !== "undefined" && typeof(window.crypto.getRandomValues) !== "undefined") {
-            let buf = new Uint32Array(4),
-                i = 0;
-            window.crypto.getRandomValues(buf);
-            return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
-                let r = (buf[i >> 3] >> ((i % 8) * 4)) & 0xf,
-                    v = c === "x" ? r : (r & 0x3 | 0x8);
-                i++;
-                return v.toString(16);
-            });
-        } else {
-            return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
-                let r = Math.random() * 16 | 0,
-                    v = c === "x" ? r : (r & 0x3 | 0x8);
-                return v.toString(16);
-            });
-        }
-    },
-
+        const buf = new Uint32Array(4).map(() => {
+            return crypto.randomBytes(4).readUInt32BE(0, true);
+        });
+        let i = 0;
+        return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
+            let r = (buf[i >> 3] >> ((i % 8) * 4)) & 0xf,
+                v = c === "x" ? r : (r & 0x3 | 0x8);
+            i++;
+            return v.toString(16);
+        });
+    }
 };
 };
 
 
 export default UUID;
 export default UUID;