10年05月03日 Monday , 326 次点击 , 评论

一般情况下,如果能找到可用的证书,就可以直接使用,只不过会因证书的某些信息不正确或与部署证书的主机不匹配而导致浏览器提示证书无效,但这并不影响使用。

需要手工生成证书的情况有:

  1. 找不到可用的证书
  2. 需要配置双向SSL,但缺少客户端证书
  3. 需要对证书作特别的定制

首先,无论是在Linux下还是在Windows下的Cygwin中,进行下面的操作前都须确认已安装OpenSSL软件包。

1. 创建根证书密钥文件root.key

openssl genrsa -des3 -out root.key

输出内容为:

[lenin@archer ~]$ openssl genrsa -des3 -out root.key
Generating RSA private key, 512 bit long modulus
……………..++++++++++++
..++++++++++++
e is 65537 (0×10001)
Enter pass phrase for root.key: ← 输入一个新密码
Verifying – Enter pass phrase for root.key: ← 重新输入一遍密码

2. 创建根证书的申请文件root.req

openssl req -new -key root.key -out root.req

输出内容为:

[lenin@archer ~]$ openssl req -new -key root.key -out root.req
Enter pass phrase for root.key: ← 输入前面创建的密码
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [AU]:CN ← 国家代号,中国输入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不输入
Common Name (eg, YOUR name) []: ← 此时不输入
Email Address []:admin@mycompany.com ← 电子邮箱,可随意填

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入

3. 创建一个自当前日期起为期十年的根证书root.crt

openssl x509 -req -days 3650 -sha1 -extensions v3_ca -signkey root.key -in root.req -out root.crt

输出内容为:

[lenin@archer ~]$ openssl x509 -req -days 3650 -sha1 -extensions v3_ca -signkey root.key -in root.req -out root.crt
Signature ok
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./emailAddress=admin@mycompany.com
Getting Private key
Enter pass phrase for root.key: ← 输入前面创建的密码

4. 创建服务器证书密钥server.key

openssl genrsa -out server.key 2048

输出内容为:

[lenin@archer ~]$ openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
….+++
…………………………………………..+++
e is 65537 (0×10001)

5.创建服务器证书的申请文件server.req

openssl req -new -key server.key -out server.req

输出内容为:

[lenin@archer ~]$ openssl req -new -key server.key -out server.req
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [AU]:CN ← 国家名称,中国输入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省名,拼音
Locality Name (eg, city) []:BeiJing ← 市名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不输入
Common Name (eg, YOUR name) []:www.mycompany.com ← 服务器主机名,若填写不正确,浏览器会报告证书无效,但并不影响使用
Email Address []:admin@mycompany.com ← 电子邮箱,可随便填

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入

6. 创建自当前日期起有效期为期两年的服务器证书server.crt

openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in server.req -out server.crt

输出内容为:

[lenin@archer ~]$ openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in server.req -out server.crt
Signature ok
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com
Getting CA Private Key
Enter pass phrase for root.key: ← 输入前面创建的密码

7. 创建客户端证书密钥文件client.key

openssl genrsa -des3 -out client.key 2048

输出内容为:

[lenin@archer ~]$ openssl genrsa -des3 -out client.key 2048
Generating RSA private key, 2048 bit long modulus
……………………………………………………………………………..+++
……………………………………………………………………………………………………….+++
e is 65537 (0×10001)
Enter pass phrase for client.key: ← 输入一个新密码
Verifying – Enter pass phrase for client.key: ← 重新输入一遍密码

8. 创建客户端证书的申请文件client.req

openssl req -new -key client.key -out client.req

输出内容为:

[lenin@archer ~]$ openssl genrsa -des3 -out client.key 2048
Generating RSA private key, 2048 bit long modulus
……………………………………………………………………………..+++
……………………………………………………………………………………………………….+++
e is 65537 (0×10001)
Enter pass phrase for client.key:
Verifying – Enter pass phrase for client.key:
[lenin@archer ~]$ openssl req -new -key client.key -out client.req
Enter pass phrase for client.key: ← 输入上一步中创建的密码
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [AU]:CN ← 国家名称,中国输入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省名称,拼音
Locality Name (eg, city) []:BeiJing ← 市名称,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不填
Common Name (eg, YOUR name) []:Lenin ← 自己的英文名,可以随便填
Email Address []:admin@mycompany.com ← 电子邮箱,可以随便填

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不填
An optional company name []: ← 可以不填

9. 创建一个自当前日期起有效期为两年的客户端证书client.crt

openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in client.req -out client.crt

输出内容为:

[lenin@archer ~]$ openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in client.req -out client.crt
Signature ok
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com
Getting CA Private Key
Enter pass phrase for root.key: ← 输入上面创建的密码

10. 将客户端证书文件client.crt和客户端证书密钥文件client.key合并成客户端证书安装包client.pfx

openssl pkcs12 -export -in client.crt -inkey client.key -out client.pfx

输出内容为:

[lenin@archer ~]$ openssl pkcs12 -export -in client.crt -inkey client.key -out client.pfx
Enter pass phrase for client.key: ← 输入上面创建的密码
Enter Export Password: ← 输入一个新的密码,用作客户端证书的保护密码,在客户端安装证书时需要输入此密码
Verifying – Enter Export Password: ← 确认密码

11. 保存生成的文件备用,其中server.crtserver.key是配置单向SSL时需要使用的证书文件,client.crt是配置双向SSL时需要使用的证书文件,client.pfx是配置双向SSL时需要客户端安装的证书文件

分类 : 计算机
Top
10年04月24日 Saturday , 374 次点击 , 4 条评论

在使用wiki的问题上,我是个不敏锐的人,虽然我敏锐地感觉到我迟早要用这个东西,但在内心里,我一直在抗拒这个感觉。

之所以说不敏锐,是因为从一开始,我就没想明白既然已经有了博客为什么还需要这么个东西。如果不是最近要学的东西实在太多,也许我还在迷糊。事实上,我突然哲人般地总结出如下推论:

一个人的一生可以划分为四个阶段:知本庸俗积累阶段→领域分化总结阶段→知本爆炸阶段→反思布道阶段。

就像我们中的很多人都在小学作文中拾到过一分、五分甚至七分不等的硬币并交给警察叔叔一样,我想这些人都对七大姑八大姨说过长大要当科学家,虽然基本上没人知道那究竟是个什么玩意儿。但不管怎样,即便是传说中的清华北大也总要收一帮黑头发的孩子哄那帮白头发的孩子玩儿,于是大家都有学上,然后很多人就真的以为自己是科学家了,当然,更多的是被以为。这就是知本的庸俗积累阶段。

大部分人在这个阶段就废了。

自以为的也好,被已为的也罢,时间长了,习惯就成了存在。当一个人真正对自己所做的事认了真,他就会有一些系统的心得、经验和教训,有些人总结得好,就成了叫兽、砖家,次一等的成了作家,最不济的都去写博客了。这就是领域分化总结阶段。

这个阶段就像计算机安全防御体系中的蜜罐系统,你真的以为自己进去了吗?很不幸,大部分人都因此而停滞不前。

所幸有些人虽然没突破蜜罐,但他们感觉到了不对,虽然很莫名。为了解疑这个莫名,这些人开始到处翻书,连马桶中带字的手纸都不放过。可惜所有的地方都只记载了真相拼图中的一个残片,于是他们用这些残片贴满了一堵名叫wiki的墙。这就是知本爆炸阶段。

可惜,很多人没熬过这个阶段,就死了。

极少的人躲过了三氯奶粉,闯过了地沟油,居然也没被高房价压死,还成功地把真相残片全部贴上了wiki墙,不用怀疑,这就是上帝的选民。然后这些人会对自己所从事的东西产生反思,和第二阶段的总结不同,这时的反思近乎道了。这就是反思布道阶段。

好吧,我承认,我想说的是,我已经开始贴wiki墙了……

早在做WinSlave的年代就试图用一个叫网博士的程序保存网页资料,算是知识组织的雏形,但那个东西不是跨平台的,所以也就随着我的Win时代的终结而渐行渐远。后来时不时地会用Firefox的Scrapbook,但一来我实在不喜欢Firefox的臃肿,虽然我不得不总是用它;二来这个东西不方便在多台电脑之间做同步。后来主要用FreeMind和Gmail的标签功能,但也时常感觉组织松散、多有不便。

直到积累知识片段的速度超过了博客更新的速度,我才发现,原来wiki就是干这事儿的。

在选用wiki系统的问题上,我考虑了一个星期。排除了VimWiki,虽然我爱Vim胜过别人的生命,但我还是希望能有代码片段语法高亮(虽然Vim能实现,但很麻烦)和插图显示功能。最后选择了DokuWiki,特性在这里

目前我还不打算把wiki放在博客主机上,毕竟我是用它来组织知识片段。经过对博客存留的短暂的思考,我觉得wiki和博客并不冲突,wiki是给自己看的,博客是用来分享的,写wiki和写博客应当采用不同的写作方式,因此,wiki上组织好的、主题明确突出的、内容成熟的东西经过整理,就可以发布到博客上,这对博客的质量也是个提升。

分类 : 青梅煮酒
Top
10年04月24日 Saturday , 247 次点击 , 评论

PHP最大的特点在于它拥有一个丰富、灵活、强大的函数库,因此得以机动灵活成为软件开发语言中的轻骑兵。

php.js是一个用Javascript实现的函数库,它试图用Javascript最大程度地重写PHP的函数库。相对于JQuery、ExtJS这些Javascript库,php.js不并致力于为AJAX、DOM和界面开发提供一揽子解决方案,它只是将PHP函数的强大和简便带到前端开发中来,由于JQuery这样的库并不旨在提供完全的Javascript开发标准和手段,实际上,它弥补了这些高端的工具库与低端的Javascript开发之间的一个空白。

对于熟悉PHP的人尤其是PHP程序员来说,使用php.js是几乎不需要切换思维方式的。

这是使用php实现的日期格式校验函数:

PHP:
  1. function IsValidDate($strDate, $strFormat='Y-m-d') {
  2.     $strDate = trim($strDate);
  3.     $unixTime = strtotime($strDate);
  4.     $strNewDate = date($strFormat, $unixTime);
  5.     return $strDate == $strNewDate;
  6. }

这是使用php.js实现的日期格式校验函数:

JAVASCRIPT:
  1. function IsValidDate(strDate, strFormat) {
  2.     strDate = trim(strDate);
  3.     var unixTime = strtotime(strDate);
  4.     var strNewDate = date(strFormat, unixTime);
  5.     return strDate == strNewDate;
  6. }

Tags :
分类 : 编程
Top
10年04月17日 Saturday , 1,260 次点击 , 4 条评论

The Pragmatic Programmer》的确是本好书,虽然中文译名极为恶俗。为践行书中提到的DRY原则,我开始反思平时编码时总是重复出现的问题,其中之一就是很低级的语法错误,有时忘了语句最后的分号,有时忘了声明变量,凡此种种,不一而足。

相对于IDE,我更喜欢Vim,这也是语法错误没有检查出来的原因。因此就产生了给Vim添加语法检查功能的需求。下面是检查PHP代码的vimrc内容:

VIM:
  1. " Check the syntax of a PHP file
  2. function! CheckPHPSyntax()
  3.     if &filetype != 'php'
  4.         echohl WarningMsg | echo 'This is not a PHP file !' | echohl None
  5.         return
  6.     endif
  7.     setlocal makeprg=php\ -l\ -n\ -d\ html_errors=off\ %
  8.     setlocal errorformat=%m\ in\ %f\ on\ line\ %l
  9.     echohl WarningMsg | echo 'Syntax checking output:' | echohl None
  10.     if &modified == 1
  11.         silent write
  12.     endif
  13.     silent make
  14.     clist
  15. endfunction
  16. au filetype php map <F5> :call CheckPHPSyntax()<CR>
  17. au filetype php imap <F5> <ESC>:call CheckPHPSyntax()<CR>

以上脚本为Vim添加了一个检查PHP语法错误的函数和两个快捷键映射,此后可按F5键即时检查当前Buffer中的PHP当面的语法。和网上其它同类的函数相比,这个函数的优点在于改进了检查结果的显示,使用silent命令隐藏了所有不必要的输出。

以下是检查Python代码语法错误的脚本:

VIM:
  1. " Check the syntax of a python file
  2. function! CheckPythonSyntax()
  3.     if &filetype != 'python'
  4.         echohl WarningMsg | echo 'This is not a Python file !' | echohl None
  5.         return
  6.     endif
  7.     setlocal makeprg=python\ -u\ %
  8.     set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
  9.    echohl WarningMsg | echo 'Syntax checking output:' | echohl None
  10.    if &modified == 1
  11.        silent write
  12.    endif
  13.    exec "silent make -c \"import py_compile;py_compile.compile(r'".bufname("%")."')\""
  14.     clist
  15. endfunction
  16. au filetype python map <F5> :call CheckPythonSyntax()<CR>
  17. au filetype python imap <F5> <ESC>:call CheckPythonSyntax()<CR>

上面两部分的配置脚本中虽然都指定了使用F5检查代码语法,但由于使用了au filetype指定了相应的语言类型,因此没有冲突。

和PHP相比,Python似乎更受垂青,因为除了上面和PHP一样的语法检查方式,它还有令人惊艳的PyFlakes 1

PyFlakes的意思是Make on the fly,与另一神器Emacs的Flymake遥相呼应。只需要举出PyFlakes的两三个特性,就足以说明这是怎样的一个尤物了:其一是实时和高效,PyFlakes会在输入代码的同时检查语法错误,而且用户丝毫感觉不到任何停顿(对于游戏玩家或高清电影狂可能“卡”更好理解);其二是智能,它居然能检查出哪些导入的模块没有被使用,哪些被使用的模块没有被导入;其三,PyFlakes检查出语法错误后会使用红色波浪线标识出错误位置。它使Vim完成了一个华丽的转身。

此外,由于PyFlakes是通过解析代码来检查语法错误,因此不必担心代码会被实际执行。

需要说明的是,PyFlakes要求Vim在编译时启用了对Python的支持,这一点可以使用:ver命令查看,一般各Linux发行版自带的Vim都加入了这个特性,而Windows下的版本没有此特性的可能性较大,好在作者提供了加入Python特性的Vim的Windows编译版

既然事已如此,不妨一不做、二不休,为Vim加上即时执行代码的功能。这个想法由来已久,出于和寻找phpsh同样的原因,我希望能在Vim中临时输入小块代码,然后即时执行并查看结果;或者即时执行正在编写的Python模块或程序。以下脚本实现了随手打开一个新的分割窗口并创建一个临时的脚本文件的功能:

VIM:
  1. " Open a temporary PHP file in a new window
  2. function! PHPSandBox()
  3.     let tmpfile = tempname().'.php'
  4.     exe 'new '.tmpfile
  5.     call setline('.', '<?php')
  6.     normal o
  7.     normal o
  8.     call setline('.', '?>')
  9.     normal k
  10.     startinsert
  11. endfunction
  12. " Open a temporary Python file in a new window
  13. function! PySandBox()
  14.     let tmpfile = tempname().'.py'
  15.     exe 'new '.tmpfile
  16.     call setline('.', '#!/usr/bin/python')
  17.     normal o
  18.     call setline('.', '# -*- coding: utf-8 -*-')
  19.     normal o
  20.     startinsert
  21. endfunction
  22. nmap <leader>sbpy :call PySandBox()<CR>
  23. nmap <leader>sbph :call PHPSandBox()<CR>

下面的脚本则实现了即时执行当前Buffer中代码的功能:

VIM:
  1. " Run a PHP script
  2. function! ExecutePHPScript()
  3.     if &filetype != 'php'
  4.         echohl WarningMsg | echo 'This is not a PHP file !' | echohl None
  5.         return
  6.     endif
  7.     setlocal makeprg=php\ -f\ %
  8.     setlocal errorformat=%m\ in\ %f\ on\ line\ %l
  9.     echohl WarningMsg | echo 'Execution output:' | echohl None
  10.     if &modified == 1
  11.         silent write
  12.     endif
  13.     silent make
  14.     clist
  15. endfunction
  16. au filetype php map <C-F5> :call ExecutePHPScript()<CR>
  17. au filetype php imap <C-F5> <ESC>:call ExecutePHPScript()<CR>
  18.  
  19. " Run a python script
  20. function! ExecutePythonScript()
  21.     if &filetype != 'python'
  22.         echohl WarningMsg | echo 'This is not a Python file !' | echohl None
  23.         return
  24.     endif
  25.     setlocal makeprg=python\ -u\ %
  26.     set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
  27.    echohl WarningMsg | echo 'Execution output:' | echohl None
  28.    if &modified == 1
  29.        silent write
  30.    endif
  31.    silent make
  32.    clist
  33. endfunction
  34. au filetype python map <C-F5> :call ExecutePythonScript()<CR>
  35. au filetype python imap <C-F5> <ESC>:call ExecutePythonScript()<CR>

Foot Note 1 :

PyFlakes本身是个通用的工具,其作者提供了一个Vim的插件调用PyFlakes的功能,故此处所说的PyFlakes实指Vim的PyFlakes插件。

分类 : 计算机
Top
10年04月10日 Saturday , 394 次点击 , 1 条评论

发现这个图标主题有段时间了,但直到今天偶然重新启用它,才突然觉得这套色调灰暗、风格搞怪的图标其实整体效果很好,很有个性,且极为闷骚,实为标榜非主流人士居家旅行必备。

Tags :
分类 : 计算机
Top