Created
August 10, 2012 03:43
-
-
Save ywjno/3310835 to your computer and use it in GitHub Desktop.
create card_no and password(10 numbers)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require 'openssl' | |
require 'base64' | |
require 'sequel' | |
DB = Sequel.connect('sqlite://cardinfo.sqlite3') | |
class CardInfo < Sequel::Model | |
plugin :schema | |
unless table_exists? | |
set_schema do | |
primary_key :id | |
String :card_no, :null=>false | |
String :password, :null=>false | |
end | |
create_table | |
end | |
end | |
class CardInfoUtils | |
def create_data | |
infos = [] | |
DB.transaction do | |
while (infos.size < 500) do | |
card_no, password = DesCoder.encode(random), DesCoder.encode(random) | |
if CardInfo.where(:card_no => card_no).count == 0 | |
infos << {:card_no => card_no, :password => password} | |
end | |
end | |
DB[:card_infos].insert_multiple(infos) | |
end | |
puts '='*80 | |
puts 'create data completed.' | |
puts '='*80 | |
end | |
private | |
def random | |
data = '' | |
pool = ('A'..'Z').to_a + ('0'..'9').to_a | |
10.times do |t| | |
length = pool.length | |
data += pool.delete_at(Random.rand(0..length - 1)) | |
end | |
data | |
end | |
module DesCoder | |
KEY = "PasswordPasswordPassword" | |
IV = "12345678" | |
CIPHER = "des-ede3-cbc" | |
URI_UNSAFE = '+/' | |
def self.encode(source) | |
return URI.encode(Base64.encode64(process(source , true)) , URI_UNSAFE).strip.tr_s("=" , "").tr_s("%" , "_") | |
end | |
def self.decode(source) | |
return process(Base64.decode64(URI.decode(source.tr_s("_" , "%")) + "=") , false) | |
end | |
private | |
def self.process(str , is_encode) | |
c = OpenSSL::Cipher::Cipher.new(CIPHER) | |
c.key = KEY | |
c.iv = IV | |
is_encode ? c.encrypt : c.decrypt | |
ret = c.update(str.to_s) | |
ret << c.final | |
end | |
end | |
end | |
CardInfoUtils.new.create_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
encoding: utf-8
500.times do |t|
card_no = ''
pool = ('A'..'Z').to_a + ('0'..'9').to_a
10.times do |t|
length = pool.length
card_no += pool.delete_at(Random.rand(0..length - 1))
end
end