Menu

Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Thursday, June 30, 2016

Generating hash using native Crypto API (Example)

You can find more information about the crypto api here:  https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto

function arrayBufferToStringAsync( /*ArrayBuffer*/ buffer) {
    var reader = new FileReader();
    return readAsync(reader, 'Text', new Blob([buffer])).then(function() {
        return new Promise(function(resolve, reject) {
            resolve(reader.result);
        });
    });
};

function arrayBufferToHexAsync( /*ArrayBuffer*/ buffer) {
    return new Promise(function(success, error) {
        arrayBufferToStringAsync(buffer).then(function(string) {
            var hex = '';
            for (var i = 0; i < string.length; i++) {
                var char = ('000' + string.charCodeAt(i).toString(16)).slice(-4);
                hex += char;
            }

            success(hex);
        }).catch(error);
    });
};

function readAsync( /* FileReader */ reader, as, data, encoding) {
    return new Promise(function(resolve, reject) {
        reader.onloadend = resolve;
        reader.onerror = reject;
        reader['readAs' + as](data, encoding);
    });
};

var blob = new Blob(['Hello World']);
var reader = new FileReader();

readAsync(reader, 'ArrayBuffer', blob).then(function() {
    var result = reader.result;
    return crypto.subtle.digest("SHA-1", result);
}).then(function(hash) {
    return arrayBufferToHexAsync(hash);
}).then(function(hexHash) {
    console.log(hexHash);
});

Sunday, February 16, 2014

Is NodeJS the best solution for your project

Ever since all the commotion around NodeJS started, I was wondering, if javascript is really good for backend development. So I started looking up various subjects regarding NodeJS, like how to configure a production server, benchmarks and others. After doing some reading I was able to form an opinion.

Thursday, February 13, 2014

Create a jquery extension for handling dom insert events

Very often when I'm building a web application, I need to dynamically create some objects and append them to the dom. Well thats ok, but if there are some additional calculations that you need to do after inserting your objects, then things are getting a bit messy. For example you need to position your objects according to a relative parent object's dimensions or initialize a plugin that requires your object to appended to the dom.  Well there is always a way of getting around issues like that, but I wanted to keep thing simple and to do that I needed some king of "onDomInsert" event.


Friday, March 15, 2013

JQuery UI resizable layout


I've created a plugin for jquery ui that enables developers to create layouts that can be resized by the users, and brings a more functional frontend design.
You will need git to download it. The repository url is https://github.com/lexmihaylov/jquery.ui.layout

Object Oriented JavaScript Using JQuery

Javascript becomes more and more popular these days, and what best way to write reusable code if not writing it the OO style. JS is not your tipical programming language, the classes in js are not actualy classes, but funcions. Every function has a prototype object that contains methods and properties, we can use this object to write javascript classes.
1. Creating a JS Class:
To create a js class you need to create a function and after that you can use the this keyword to access the object's properties and methods.