From 4a00c3b7a7e3934d65a357c2f763b9310b6f7946 Mon Sep 17 00:00:00 2001
From: AlexanderWillner <alexander.willner@tu-berlin.de>
Date: Wed, 20 Feb 2013 15:56:27 +0100
Subject: [PATCH] added browser check

---
 resources/example.html |  2 +-
 resources/example.js   | 30 ++++++++++++++++++++++++++----
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/resources/example.html b/resources/example.html
index f2aaf6ad..12d7f23e 100644
--- a/resources/example.html
+++ b/resources/example.html
@@ -1,6 +1,7 @@
 <!doctype html>
 <html>
  <head>
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>Example | OpenPGP.js</title>
   <link rel="stylesheet" media="all" href="example.css">
  </head>
@@ -65,7 +66,6 @@ z8jl9UymAkoTcsVWRVr5PKR0zsCxaNV3w5hv6j12+US7mEWe2kuD
    </pre>
   </div>
   <script src="./jquery.min.js" type="text/javascript" charset="utf-8"></script>
-  <script src="./openpgp.min.js" type="text/javascript" charset="utf-8"></script>
   <script src="./example.js" type="text/javascript" charset="utf-8"></script>
  </body>
 </html>
diff --git a/resources/example.js b/resources/example.js
index b19e0938..e9467228 100644
--- a/resources/example.js
+++ b/resources/example.js
@@ -1,6 +1,28 @@
 function encrypt() {
-  openpgp.init();
-  var pub_key = openpgp.read_publicKey($('#pubkey').text());
-  $('#message').val(openpgp.write_encrypted_message(pub_key,$('#message').val()));
-  window.alert("This message is going to be sent:\n" + $('#message').val());
+  if (window.crypto.getRandomValues) {
+    require("./openpgp.min.js");
+    openpgp.init();
+    var pub_key = openpgp.read_publicKey($('#pubkey').text());
+    $('#message').val(openpgp.write_encrypted_message(pub_key,$('#message').val()));
+    window.alert("This message is going to be sent:\n" + $('#message').val());
+    return true;
+  } else {
+    $("#mybutton").val("browser not supported");
+    window.alert("Error: Browser not supported\nReason: We need a cryptographically secure PRNG to be implemented (i.e. the window.crypto method)\nSolution: Use Chrome >= 11, Safari >= 3.1 or Firefox >= 21");   
+    return false;
+  }
+}
+
+function require(script) {
+    $.ajax({
+        url: script,
+        dataType: "script",
+        async: false,           // <-- this is the key
+        success: function () {
+            // all good...
+        },
+        error: function () {
+            throw new Error("Could not load script " + script);
+        }
+    });
 }