Example of RSA using JAVA

Scenario

I have two web services and I want to send encrypted password as HTTP response.

Solution

Implement asymmetric cryptography using RSA as the following flow chart.

flow-of-asymmetric-cryptography-001

Here is the source code of RSA cryptography.

The difference CSS selector *= and ~=

Description

I was considering what the difference between *= and ~= and what is the better situation for the each selector.

The below descriptions are both from CSS Selector.

E[foo~=”bar”] an E element whose “foo” attribute value is a list of whitespace-separated values, one of which is exactly equal to “bar” Attribute selectors 2
E[foo*=”bar”] an E element whose “foo” attribute value contains the substring “bar” Attribute selectors 3

For example, we have a HTML and a CSS file on Gist. Enjoy the DEMO. ūüôā

 

 

Blob In Database Using JPA

Story

My aim is creating a web service for responding as File whose type is oracle.sql.BLOB.

Oh! I could barely find the problem which always shows could not deserialize. At first, I read the section and knew that it was caused by composite key because I did not create both hashCode and equals functions. But it still not worked. Why? See Solution.

I finally inspired by this post.

check whether they are serializable itself and all their fields either serializable too

So that I started searching the method of JPA for Blob and I found this. It described how to¬†insert and retrieve¬†Blob type. Anyway, let’s see the sample.

Sample

Retrieve File From Google Drive

Scenario

I want to retrieve public(shared with anyone and can be found) files without authentication(Google Auth) and I was using drive.files.list Google Drive API v3.

Google Drive Using YQL.001Google Drive Using YQL.002

As you can see, Google API always refused the requests sent by browsers because it needs OAuth. 

Solution

I realized that¬†can get file separately instead because drive.files.get doesn’t need OAuth if¬†the files are public. And it only return file content in Google Drive API v2. It’s being much easier. ūüėÄ

By the way, we still have to apply API key.

Why Not Google Drive API v3?

The response doesn’t include file content anymore despite Google Drive API v3 migrated from Google Drive API v2.

Check is first-page-load using Cookie

Scenario

If we run some JavaScript code when the page has been loaded at first time only. For example, we want to show promotion for the users visit our website first time.

    if(isFirstTimeVisit) {
        // Show promotion.
    }

How to implement? Let’s see the following solution.

Solution

First of all, we have to know how to operate Cookie. I found a way from CSS TRICKS.

    var _ = {}; // Declare underscore as an Object.
    /**
     * Gets or sets cookies
     * @param name
     * @param value (null to delete or undefined to get)
     * @param options (domain, expire (in days))
     * @return value or true
     */
    _.cookie = function(name, value, options)
    {
        if (typeof value === "undefined") {
            var n, v,
                cookies = document.cookie.split(";");
            for (var i = 0; i < cookies.length; i++) {
                n = $.trim(cookies[i].substr(0,cookies[i].indexOf("=")));
                v = cookies[i].substr(cookies[i].indexOf("=")+1);
                if (n === name){
                    return unescape(v);
                }
            }
        } else {
            options = options || {};
            if (!value) {
                value = "";
                options.expires = -365;
            } else {
                value = escape(value);
            }
            if (options.expires) {
                var d = new Date();
                d.setDate(d.getDate() + options.expires);
                value += "; expires=" + d.toUTCString();
            }
            if (options.domain) {
                value += "; domain=" + options.domain;
            }
            if (options.path) {
                value += "; path=" + options.path;
            }
            document.cookie = name + "=" + value;
        }
    };

Then, declare variable hasLoadedBefore and set it true.

    _.cookie('hasLoadedBefore', true);

At last, check the variable hasLoadedBefore when you need. See the following demo.