Permalink
Browse files

Initial working commit

  • Loading branch information...
0 parents commit 5f8d78161b74817a0afe66ed7c4bf4f6100b6708 @gf3 committed Mar 14, 2012
Showing with 705 additions and 0 deletions.
  1. +9 −0 .gitignore
  2. +31 −0 README.md
  3. +17 −0 project.clj
  4. +119 −0 resources/crypt.js
  5. +54 −0 resources/hash.js
  6. +428 −0 resources/md5.js
  7. +41 −0 src/clavatar_js/core.cljs
  8. +6 −0 test/clavatar_js/test/core.clj
@@ -0,0 +1,9 @@
+/pom.xml
+*jar
+/lib
+/classes
+/native
+/.lein-failures
+/checkouts
+/.lein-deps-sum
+/.lein*
@@ -0,0 +1,31 @@
+# clavatar-js
+
+A ClojureScript port of [Rayne's clavatar](https://github.com/Raynes/clavatar). This is a library to generate Gravatar URLs from email addresses.
+
+## Usage
+
+In your `project.clj`:
+
+```clojure
+[clavatar-js "0.1.0-SNAPSHOT"]
+```
+
+In your app:
+
+```clojure
+cljs.user> (clavatar-js.core/gravatar "gianni@runlevel6.org")
+"https://secure.gravatar.com/avatar/4b0209ae3652cc5a7d53545e759fbe39?s=50&r=pg&d=retro&"
+cljs.user> (clavatar-js.core/gravatar "gianni@runlevel6.org" :size 500)
+"https://secure.gravatar.com/avatar/4b0209ae3652cc5a7d53545e759fbe39?s=500&r=pg&d=retro&"
+cljs.user> (clavatar-js.core/gravatar "gianni@runlevel6.org" :size 500 :rating :g)
+"https://secure.gravatar.com/avatar/4b0209ae3652cc5a7d53545e759fbe39?s=500&r=g&d=retro&"
+cljs.user> (clavatar-js.core/gravatar "gianni@runlevel6.org" :default :mm) ; Sets the default image to mystery man.
+"https://secure.gravatar.com/avatar/4b0209ae3652cc5a7d53545e759fbe39?s=50&r=pg&d=mm&"
+cljs.user> (clavatar-js.core/gravatar "i@raynes.me" :default :mm :size 128 :https false)
+"http://www.gravatar.com/avatar/54222b6321f0504e0a312c24e97adfc1?s=128&r=pg&d=mm&"
+```
+
+## License
+
+Distributed under the Eclipse Public License, the same as Clojure.
+
@@ -0,0 +1,17 @@
+(defproject clavatar-js "0.1.0-SNAPSHOT"
+ :description "Gravatar for Clojurescript!"
+ :dependencies [[org.clojure/clojure "1.3.0"]]
+ :plugins [[lein-cljsbuild "0.1.2"]]
+ :cljsbuild {:builds
+ [{:source-path "src"
+ :compiler {
+ :libs ["resources/crypt.js" "resources/hash.js" "resources/md5.js"]
+ :output-to "clavatar_js.js"
+ :optimizations :simple
+ :pretty-print true}}]}
+ :url "https://github.com/gf3/clavatar-js"
+ :license {:name "Eclipse Public License - v 1.0"
+ :url "http://www.eclipse.org/legal/epl-v10.html"
+ :distribution :repo
+ :comments "same as Clojure"})
+
@@ -0,0 +1,119 @@
+// Copyright 2008 The Closure Library Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS-IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+/**
+ * @fileoverview Namespace with crypto related helper functions.
+ */
+
+goog.provide('goog.crypt');
+
+goog.require('goog.array');
+
+
+/**
+ * Turns a string into an array of bytes; a "byte" being a JS number in the
+ * range 0-255.
+ * @param {string} str String value to arrify.
+ * @return {Array.<number>} Array of numbers corresponding to the
+ * UCS character codes of each character in str.
+ */
+goog.crypt.stringToByteArray = function(str) {
+ var output = [], p = 0;
+ for (var i = 0; i < str.length; i++) {
+ var c = str.charCodeAt(i);
+ while (c > 0xff) {
+ output[p++] = c & 0xff;
+ c >>= 8;
+ }
+ output[p++] = c;
+ }
+ return output;
+};
+
+
+/**
+ * Turns an array of numbers into the string given by the concatenation of the
+ * characters to which the numbers correspond.
+ * @param {Array} array Array of numbers representing characters.
+ * @return {string} Stringification of the array.
+ */
+goog.crypt.byteArrayToString = function(array) {
+ return String.fromCharCode.apply(null, array);
+};
+
+
+/**
+ * Turns an array of numbers into the hex string given by the concatenation of
+ * the hex values to which the numbers correspond.
+ * @param {Array} array Array of numbers representing characters.
+ * @return {string} Hex string.
+ */
+goog.crypt.byteArrayToHex = function(array) {
+ return goog.array.map(array, function(numByte) {
+ var hexByte = numByte.toString(16);
+ return hexByte.length > 1 ? hexByte : '0' + hexByte;
+ }).join('');
+};
+
+
+/**
+ * Converts a JS string to a UTF-8 "byte" array.
+ * @param {string} str 16-bit unicode string.
+ * @return {Array.<number>} UTF-8 byte array.
+ */
+goog.crypt.stringToUtf8ByteArray = function(str) {
+ // TODO(user): Use native implementations if/when available
+ str = str.replace(/\r\n/g, '\n');
+ var out = [], p = 0;
+ for (var i = 0; i < str.length; i++) {
+ var c = str.charCodeAt(i);
+ if (c < 128) {
+ out[p++] = c;
+ } else if (c < 2048) {
+ out[p++] = (c >> 6) | 192;
+ out[p++] = (c & 63) | 128;
+ } else {
+ out[p++] = (c >> 12) | 224;
+ out[p++] = ((c >> 6) & 63) | 128;
+ out[p++] = (c & 63) | 128;
+ }
+ }
+ return out;
+};
+
+
+/**
+ * Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
+ * @param {Array.<number>} bytes UTF-8 byte array.
+ * @return {string} 16-bit Unicode string.
+ */
+goog.crypt.utf8ByteArrayToString = function(bytes) {
+ // TODO(user): Use native implementations if/when available
+ var out = [], pos = 0, c = 0;
+ while (pos < bytes.length) {
+ var c1 = bytes[pos++];
+ if (c1 < 128) {
+ out[c++] = String.fromCharCode(c1);
+ } else if (c1 > 191 && c1 < 224) {
+ var c2 = bytes[pos++];
+ out[c++] = String.fromCharCode((c1 & 31) << 6 | c2 & 63);
+ } else {
+ var c2 = bytes[pos++];
+ var c3 = bytes[pos++];
+ out[c++] = String.fromCharCode(
+ (c1 & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
+ }
+ }
+ return out.join('');
+};
@@ -0,0 +1,54 @@
+// Copyright 2011 The Closure Library Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS-IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+/**
+ * @fileoverview Abstract cryptographic hash interface.
+ *
+ * See goog.crypt.Sha1 and goog.crypt.Md5 for sample implementations.
+ *
+ */
+
+goog.provide('goog.crypt.Hash');
+
+
+
+/**
+ * Create a cryptographic hash instance.
+ *
+ * @constructor
+ */
+goog.crypt.Hash = function() {};
+
+
+/**
+ * Resets the internal accumulator.
+ */
+goog.crypt.Hash.prototype.reset = goog.abstractMethod;
+
+
+/**
+ * Adds a byte array (array with values in [0-255] range) or a string (might
+ * only contain 8-bit, i.e., Latin1 characters) to the internal accumulator.
+ *
+ * @param {Array.<number>|Uint8Array|string} bytes Data used for the update.
+ * @param {number=} opt_length Number of bytes to use.
+ */
+goog.crypt.Hash.prototype.update = goog.abstractMethod;
+
+
+/**
+ * @return {Array.<number>} The finalized hash computed
+ * from the internal accumulator.
+ */
+goog.crypt.Hash.prototype.digest = goog.abstractMethod;
Oops, something went wrong.

0 comments on commit 5f8d781

Please sign in to comment.