루비

2016-12-22 1 views
1

내가 대문자, 소문자, 숫자, 특수 문자, 같은 것을 포함하는 루비에서 다소 읽을 수있는 단어를 만들려고 해요 다소 읽을 수있는 단어 만들기 다음과 같이 할 수 있습니다.루비

lower = ('a'...'z').to_a 
upper = ('A'...'Z').to_a 
numbers = (0...9).to_a 
specs = %w(! ? *^$) 
size = 8 

charset = [lower, upper, numbers, specs].flatten 
(0...size).map { charset[rand(charset.size)] }.join 
#<= ?!VVQUjH 
#<= ^tsm^Led 

임의의 문자열을 다소 읽을 수있는 방법이 있습니까? 상수, 모음 등을 포함합니다. 특수 문자와 외부 보석을 사용하지 않고 적어도 하나의 숫자로?

lower = ('a'..'z').to_a 
upper = ('A'..'Z').to_a 
numbers = (0..9).to_a 
specs = %w(! ? *^$) 

charset = [lower, upper, numbers, specs] 

그런 다음이 같은 Enumerable#inject을 사용할 수, 각 문자 집합이 말을하려면 말 :

+0

"읽을 수있는"문자열이 무엇인지 정확하게 설명 할 수 있습니까? 예를 들어 "^ tsm^Le2"는 모든 문자 요구 사항을 충족하지만 ... 읽을 수 있다고 부릅니까? –

+1

나는 다른 방법으로 가서 실제 단어를 가져 와서 ("l '->'1 '등) –

+0

@HenningKockerbeck 그걸 취하는 재미있는 관점입니다. 아이디어를 주셔서 감사합니다. – User9123

답변

1

는 근거로이 gist를 사용하여 스크래치

에서 비밀번호를 생성 :

class PasswordGenerator 
    # Original version : https://gist.github.com/lpar/1031933 
    # Simple generation of readable random passwords using Ruby 
    # by lpar 

    # These are the koremutake syllables, plus the most common 2 and 3 letter 
    # syllables taken from the most common 5,000 words in English, minus a few 
    # syllables removed so that combinations cannot generate common rude 
    # words in English. 
    SYLLABLES = %w(ba be bi bo bu by da de di do du dy fe fi fo fu fy ga ge gi 
     go gu gy ha he hi ho hu hy ja je ji jo ju jy ka ke ko ku ky la le li lo 
     lu ly ma me mi mo mu my na ne ni no nu ny pa pe pi po pu py ra re ri ro 
     ru ry sa se si so su sy ta te ti to tu ty va ve vi vo vu vy bra bre bri 
     bro bru bry dra dre dri dro dru dry fra fre fri fro fru fry gra gre gri 
     gro gru gry pra pre pri pro pru pry sta ste sti sto stu sty tra tre er 
     ed in ex al en an ad or at ca ap el ci an et it ob of af au cy im op co 
     up ing con ter com per ble der cal man est for mer col ful get low son 
     tle day pen pre ten tor ver ber can ple fer gen den mag sub sur men min 
     out tal but cit cle cov dif ern eve hap ket nal sup ted tem tin tro tro) 

    def initialize 
    srand 
    end 

    def generate(length) 
    result = '' 
    while result.length < length 
     syl = SYLLABLES[rand(SYLLABLES.length)] 
     result += syl 
    end 
    result = result[0,length] 
    # Stick in a symbol 
    spos = rand(length) 
    result[spos,1] = %w(! ? *^$).sample 
    # Stick in a digit, possibly over the symbol 
    dpos = rand(length) 
    result[dpos,1] = rand(9).to_s 
    # Make a letter capitalized 
    cpos = rand(length) 
    result[cpos,1] = result[cpos,1].swapcase 
    return result 
    end 
end 

pwgen = PasswordGenerator.new 

10.times do 
    puts pwgen.generate(10) 
end 

출력 예 :

%x(pwgen).chomp 
#=> "aipeebie" 

더 복잡한 암호

:

%x(pwgen --symbols --numerals --capitalize #{length}).chomp 
#=> "Ma[Foh1EeX" 

올바른 말 배터리 주식

가 이어 pwgen을

사용

Mageve$7ur 
Pehu5ima^r 
a!hub8osta 
b5gobrY^er 
miN!e3inyf 
manb1ufr^p 
d^m0ndeRca 
Ter5esur$b 
m8r$omYket 
gyso5it^le 

당신은 또한 단지 pwgen 및 사용을 설치할 수 이 XKCD의 권장 사항 마이크를 사용하면 사전에서 일반 단어 4 개를 선택할 수 있습니다 (예 : /usr/share/dict/american-english).

0

.. 대신 ...를 사용하여 약간 범위를 수정, 이것은 각 범위의 마지막 요소를 포함 :

charset.inject([]) { |memo,obj| memo + obj.sample(2) }.shuffle.join 
#=> "Lf5^$O2u" 

뭔가 이상의 사용자 정의 :

def rand_string (l,u,n,s) 
    (('a'..'z').to_a.sample(l) + 
    ('A'..'Z').to_a.sample(u) + 
    (0..9).to_a.sample(n)  + 
    %w(! ? *^$).sample(s)).shuffle.join 
end 

6,그런 다음 3 개 낮은 문자, 3 대문자, 3 개 개의 숫자와 1 개 특수 문자와 가중 임의의 문자열은 다음과 같습니다

rand_string(3,3,3,1) 
#=> "ic!I04OEr7"