PageTalks
Home
2012 Jan 19

Wrangling with Deflate, Base64 and GZip

最近不小心被一些编码相关的东西困扰了。这些从Web诞生便产生的一些编码问题,在2012年的今天仍然可以让一个程序员抓狂。项目的大致需求是这样的,客户端需要将一个字符串进行如下处理:

  1. 将字符串进行Deflate压缩
  2. 将压缩后的结果再进行Base64编码

那么服务器端,需要进行上述流程的反过程。问题是,由于这是个开放接口,客户端可以使用各种不同的平台进行开发,如JS、Java、Ruby、PHP等。好在,大多数语言都是基于zlib包装了自己的相关API,那么也算是实现标准了。窘迫的事情时,NodeJS里面zlib的实现是在0.6以后加入的,我们使用的技术仍然是0.4版本的。

迫于时间有限,切换到0.6或者实现zlib的binding都是不靠谱的,于是我打开了万能的Github。果然功夫不负有心人,找到了Deflate的纯JS实现——RawDeflate。顿时对作者的崇拜之心油然而生。殊不知,悲剧就是从这里开始的。 Read More

2012 Jan 18

More About Crypto Module In NodeJS

上一篇文章里讨论了crypto模块里的一些常用方法。在0.6系列以后,crypto模块的改动非常大,增添了DiffieHellman、pbkdf2、randomByes三大块。这三个也是非常有趣的东西,在这里和大家分享一下。

DiffieHellman

看着名字很恐怖,不过却是很有趣的东西。有没有想过,大家彼此不告诉对方敏感信息,确能够共同知道一个密钥?DiffieHellman算法就可以实现这一点。

The Diffie–Hellman key exchange method allows two parties that have no prior knowledge of each other to jointly establish a shared secret key over an insecure communications channel.

亮点在加粗的部分,密钥交换方法,而且不需要走https或者其他专用链接。wiki文章已经对该算法做了很详细的解释,我也仿造这里面的说明,做了一个简单的实现,希望能阐明其工作方式。NodeJS的实现有如下特点:

  • 算法中的初始根(Primitive Root)始终是2
  • 算法中可以帮你选择Private Integer和Private Key,详见generateKEys方法
  • 如果不指明,生成的密钥、公钥全都是binary格式
  • 我本想实现wiki中的那个例子,可是node的实现似乎不允许使用那么小的prime;手工计算A、B的合法性、可能性似乎也值的怀疑
var crypto = require("crypto"),
    Buffer = require("buffer").Buffer;

var alice, bob, A, a, B, b, p, s1, s2;

alice = crypto.createDiffieHellman(8);//using a 8 bits length prime
A = alice.generateKeys("hex");
a = alice.getPrivateKey("hex");//this is secret
p = alice.getPrime("hex");
console.log("Public Key of alcie: ", alice.getPublicKey("hex"), A);
console.log("Private Key of alice: ", a);
console.log("Prime: ", p); 

//sending p and A to Bob
bob = crypto.createDiffieHellman(p, "hex");//Bob should use the same prime
B = bob.generateKeys("hex");
b = bob.getPrivateKey("hex");
console.log("Public Key of bob", bob.getPublicKey("hex"), B);
console.log("Private Key of bob: ", b);
s2 = bob.computeSecret(A, "hex", "hex");

//sending B to Alice
s1 = alice.computeSecret(B, "hex", "hex");

console.log("Shared Secret:", s1, s2);

Read More

2012 Jan 14

Immigrate WordPress – A Real Case

上个星期,我终于决定把elfvision.com上的博客移到robinqu.me这个新域名上。由于使用wordpress已经很多年了,前前后后也做过不少wordpress的维护工作了,自我感觉良好,不料这次遇到不少麻烦,遂整理成文。

分析一下我这里的案例,其实是要做以下这些事情(原站点简称A,新站点简称B):

  1. 制作A在B下的镜像
  2. 修改B下面的镜像Wordpress中域名设置
  3. 对A站相关地址进行转向到B站域名
  4. 清理A站的旧文件
  5. 在A站和B站上同时发出公告,安抚、告知用户

制作站点镜像

开头很重要,如果你的站点无时无刻有访问量并产生数据,你面临两种选择:1、临时关闭站点,挂出维护通知 2、站点切换后再灌入差量数据。所幸,Wordpress站点往往基本没有实时数据,我的做法是没有关闭原站,而是直接复制数据的。

假设你可以登陆你的服务器,那么直接远程SSH就可以完成对文件的复制,即将wordpress站点的根目录直接复制到新位置。如果不能,那么就通过FTP慢慢拖吧⋯⋯

至于数据库部分,如果安装了phpMyAdmin,可以登陆到管理后台,直接选中数据库,然后在Operation选项卡中有Copy database的功能。

否则就得登陆mysql客户端,执行mysqldump了:

mysqldump [db1] -u root -ppassword --add-drop-table | mysql [db2] -u root -ppassword

Read More

2012 Jan 14

Crypto Module and Security In NodeJS

NodeJS已经在各个领域都有应用了,大部分敏感数据在公共网络上传播时都会遇到各种安全问题,在分布式系统上尤为明显。J2EE等主流应用框架都已经对HTTPS、各种加密算法有了良好支持,但是对于新型的NodeJS引擎,其版本号甚至没有达到“1”却已经掀起了业界的改革狂潮。周末抽空,来整整NodeJS里面加密和HTTPS的相关只是,希望对大家有用。

其实NodeJS的相关实现就是把C的库套了一层壳,那么JS下面的openssl等模块其实就代表了NodeJS相关的功能和性能了。编译NodeJS时务必要安装openssl模块,否则crypto模块基本不可用。

该文章的所有代码实例均是Node 0.6.4、OpenSSL 0.9.8r on MacOS Lion 64bit。演示代码的源代码都在Github上:https://github.com/RobinQu/crypto-demo

openssl-commands

Hash算法

先从最基本的Hash算法说起。以当前平台为例,支持的Digest算法不少,最常用的MD5、SHA1等。大家都知道我们这用的Hash都是不可逆的,所以这是少数几个不成对出现的方法之一,详细文档:http://nodejs.org/docs/v0.6.4/api/crypto.html#crypto.createHash

var crypto = require("crypto");

var md5hash, result;
//Warning: MD5 collision is made easier and easier. Use SHA1 instead!
md5hash = crypto.createHash("md5");
md5hash.update("hello world!");
md5hash.update("hello nodejs!");
result = md5hash.digest("hex");
console.log(result); //7d962c953bb09058460f3f47650b1ab2

HMAC算法

以前我也不怎么用HMAC,所以临时wiki了一下。HMAC结果通常是带私钥两次hash后的一串字符,用来同时验证数据完整性(data integrity)和真实性(authenticity)。wiki里面的图表什么的已经说的相当详细了。NodeJS里面的相关文档:http://nodejs.org/docs/v0.6.4/api/crypto.html#crypto.createHmac

var crypto = require("crypto");

var hmac, result;
hmac = crypto.createHmac("sha1", "i'm a secret!");

hmac.update("hello world!");
hmac.update("hello nodejs!");
result = hmac.digest("hex");
console.log(result); //9e7b9239f03d2e03cb041a8518977ac84ab4c9b9

Read More

2011 Feb 20

Flickry – Step By Step to Create A Flickr App, Chapter 4


HTML5 Powered with CSS3 / Styling, Performance & Integration, Semantics, and Offline & Storage

In the previous chapter, we finally make flickry to work in the wild. But this doesn’t satisfy me, there are still some exciting improvements that could be done!

In this chapter, we will discuss about url routing or history management in this app.

Save The URL

Web pages or apps are url-based. However, Sproutcore apps are single-page app whose url never changes. This feature may sometimes challenge the habbit of users.

Apps like Gmail have already been enhanced for url routing support, which can be used to navigate inside app with the string after “#”. e.g.

http://yourapp.com/#controller/action/parameter

The content of emphasized string won’t make browser to refresh or go to any other address, but it’s useful to send state info to your app in order to decide which page or state you really want to see.

And users can really bookmark your url and click the back and forward buttons of browser!

In Sproutcore, url routing can be done with the help from SC.routes.

Important Changes

Before going any further, I am afraid that you have to do some patches on the existing files due to my own negligence.

In short, I’ve fixed a lot wired problems in Flickry and forgot to track down the minor changes to the project. You’d better pull the latest files from brach chp4.

Now I’m going to explain some of the most crucial ones. Read More

2011 Feb 9

Flickry – Step By Step to Create A Flickr App, Chapter 3


HTML5 Powered with CSS3 / Styling, Performance & Integration, Semantics, and Offline & Storage

Bad Luck! I forgot to save the draft before quitting my blog editor. Tragically, I wrote this article twice.

It also upsets me that there were a lot post views but no responses.

Well, in this chapter we are going to make this app actually search the flickr database other than fetch fixtures locally. In addition, we will add support for url routing, which makes our app looks more interesting.

Proxy, Proxy, Proxy

Everyone knows same-origin policy in the browser sandbox. It is such an old friend to a web developer that we’ve got many workarounds, JSONP, YQL, etc.

For the time being, I don’t want make things complicated. I just want to take advantages of sc-server to proxy my responses to remote flickr server.

It’s pretty easy to configure sc-server with some proxy rules to help us achieve cross-domain ajax requests.

Open Buildfile in the root folder of our project, add the following code at the end.

proxy '/services', :to => 'api.flickr.com'

This rules instruct sc-server to route any requests heading for “/services” to re-route to “api.flickr.com”.
Read More

Page 1 of 1312345...Last »

Mobify empowers marketers and developers to create amazing mobile web experiences. Tap to learn more

Mobify