Skip to content

Instantly share code, notes, and snippets.

@chensoren
chensoren / gist:3081970
Created July 10, 2012 08:11
nodejs AES encrypt and decrypt
var crypto = require('crypto');
var AESCrypt = {};
AESCrypt.decrypt = function(cryptkey, iv, encryptdata) {
encryptdata = new Buffer(encryptdata, 'base64').toString('binary');
var decipher = crypto.createDecipheriv('aes-256-cbc', cryptkey, iv),
decoded = decipher.update(encryptdata);
@chensoren
chensoren / array_hash_form.html.erb
Last active June 20, 2017 13:59
generate an ordered array of hashes in form
<input type="text" name="linkeys[][eui]" class="sign_input">
<input type="text" name="linkeys[][code]" class="sign_input">
params[:linkeys][0] => {"code"=>"597516F5D90924764645C6D25023ECFC", "eui"=>"C8D2C10000000A03"}
# 更复杂的嵌套:
@chensoren
chensoren / gist:4407154
Created December 29, 2012 14:13
Download remote file with Paperclip gem
# Scenario 1: remote resource returns a binary file;
# the last part of the uri represents the file name
# e.g. http://someurl.com/artists/jeanlucponty/tracks/elephants-in-love.mp3
class Audio < ActiveRecord::Base
has_attached_file :file
def file_from_url(url)
self.file = download_remote_file(url)
end
@chensoren
chensoren / bin_str_to_hex.js
Last active December 17, 2015 08:39
二进制文本转换
//第0, 2, 5, 11位为1, 其它位为0
//二进制应该为 010000100101
var digitArray = ['0', '2', '5', '11'];
var binByte;
for(i=0; i<digitArray.length; i++) {
binByte = binByte | 1 << digitArray[i];
}
@chensoren
chensoren / jslider_conflict.html
Last active August 24, 2018 16:43
resolve conflict between jquery-ui-slider and jquery.slider plugin
/** both plugin use same function slider
* need rename slider method to resolve the conflicts
* */
<script type="text/javascript" src="/javascripts/jslider/jquery.slider-min.js"></script>
<script type="text/javascript" src="/javascripts/jslider/jquery.dependClass.js"></script>
<script type="text/javascript">
//resolve conflict between jslider and jquery-ui-slider
$.fn.myslider = $.fn.slider;
delete $.fn.slider;
</script>
@chensoren
chensoren / KeyBinding.json
Last active December 18, 2015 03:29
sublime text preference config
[
{ "keys": ["ctrl+shift+."], "command": "erb" },
{ "keys": ["ctrl+shift+f"], "command": "reindent"},
{ "keys": ["ctrl+super+r"], "command": "reveal_in_side_bar" }
]
@chensoren
chensoren / module_extend_include.rb
Created June 19, 2013 14:20
ruby module extend and include
module Stringify
# When a class includes a module
# the module’s self.included method will be invoked.
def self.included(base)
# Initialize module.
end
# Requires an instance variable @value
def stringify
@chensoren
chensoren / layout.html.erb
Created June 21, 2013 10:25
rails 3 content_for
<%= content_for?(:page_title) ? yield(:page_title) : default_page_title %>
@chensoren
chensoren / iso_time_local.js
Created July 2, 2013 00:36
parse ISO 8601 date string to local time
function parseISO8601(str) {
// we assume str is a UTC date ending in 'Z'
var parts = str.split('T'),
dateParts = parts[0].split('-'),
timeParts = parts[1].split('Z'),
timeSubParts = timeParts[0].split(':'),
timeSecParts = timeSubParts[2].split('.'),
timeHours = Number(timeSubParts[0]),
_date = new Date;
@chensoren
chensoren / broken_img.html
Created July 4, 2013 02:41
hide broken image
<img src="broken.png" onerror="this.style.display='none'"/>