Module: FileSize
- Included in:
- PhoenixPassword
- Defined in:
- lib/file_size.rb
Instance Method Summary collapse
- #cap_limit_combs(data, x = 0) ⇒ Object
- #cap_limit_matching(data) ⇒ Object
- #cap_limit_matching_l(data) ⇒ Object
- #create_file(data) ⇒ Object
- #file_info(data) ⇒ Object
- #get_above_limit(data) ⇒ Object
- #get_combinations(data) ⇒ Object
- #get_rule_size(data) ⇒ Object
- #get_single_combs(data) ⇒ Object
- #get_size(data) {|return_sizes| ... } ⇒ Object
- #get_strictness_one(letters, digits, cmb_length) ⇒ Object
- #unique_cap_limit(data) ⇒ Object
Instance Method Details
#cap_limit_combs(data, x = 0) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/file_size.rb', line 217 def cap_limit_combs(data,x=0) if data[:type] == "matching" if !data[:match_limit].nil? puts "Combinations and file size may vary when using match_limit" if x == 0 cap_limit_matching_l(data) else cap_limit_matching(data) end else unique_cap_limit(data) end end |
#cap_limit_matching(data) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/file_size.rb', line 23 def cap_limit_matching(data) cap_data={} total_chars=data[:characters].join() caps_matched= total_chars.scan(/[A-Z]/) cap_data[:characters]=[] data[:characters].each do |char| next if caps_matched.include?(char) cap_data[:characters].push(char) end base=cap_data[:characters].length cap_matches=0 case data[:cmb_length] when 3 cap_matches += base*2 when 4 cap_matches+=get_combinations({:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-1})*2 cap_matches+=(base**2)*2 else cap_matches+=get_combinations({:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-1})*2 cap_matches+=(get_combinations({:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-2})*base)*(data[:cmb_length]-2) end if !data[:extra_chars].nil? all_chars=data[:characters]+data[:extra_chars] new_cap_matches=cap_limit_matching({:cmb_length=>data[:cmb_length],:characters=>all_chars}) return new_cap_matches-(cap_matches*caps_matched.length) else return cap_matches*caps_matched.length end end |
#cap_limit_matching_l(data) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/file_size.rb', line 55 def cap_limit_matching_l(data) cap_data={} total_chars=data[:characters].join() caps_matched= total_chars.scan(/[A-Z]/) cap_data[:characters]=[] data[:characters].each do |char| next if caps_matched.include?(char) cap_data[:characters].push(char) end base=cap_data[:characters].length cap_matches=0 no_limit_matches=get_combinations({:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-1}) case data[:cmb_length] when 3 cap_matches += base*2 when 4 cap_matches+=(no_limit_matches-get_above_limit({:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-1,:match_limit=>data[:match_limit]}))*2 cap_matches+=(base**2)*2 else #XXA cap_matches+=(no_limit_matches-get_above_limit({:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-1,:match_limit=>data[:match_limit]}))*2 no_limit_matches=get_combinations({:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-2}) #XAX cap_matches+=((no_limit_matches-get_above_limit({:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-2,:match_limit=>data[:match_limit]}))*base)*2 x=data[:cmb_length]-4 if x == 1 cap_matches +=get_combinations({:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-2})*base elsif x == 2 grt_half_no_limit=get_combinations({:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-3}) grt_half_limit =grt_half_no_limit-get_above_limit({:characters=>cap_data[:characters],:match_limit=>data[:match_limit],:cmb_length=>data[:cmb_length]-3}) cap_matches +=(((grt_half_limit*base**2)+(base*base**3))-(grt_half_no_limit*base))*2 else #AXX i=3 p=0 l=0 begin grt_half=get_combinations({:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-(3+l)}) grt_half_limit =grt_half-get_above_limit({:characters=>cap_data[:characters],:match_limit=>data[:match_limit],:cmb_length=>data[:cmb_length]-(3+l)}) if i == 3 if data[:cmb_length] <=8 cap_matches +=(((grt_half_limit*base**2)+(base*base**(x+1)))-(grt_half*base))*2 else cap_matches +=((grt_half_limit*((base**2)-base))+(base*base**(x+1)))*2 end else lsr_half=get_combinations({:characters=>cap_data[:characters],:cmb_length=>i-1}) lsr_half_limit=lsr_half-get_above_limit(:characters=>cap_data[:characters],:match_limit=>data[:match_limit],:cmb_length=>i-1) cap_matches +=(((grt_half_limit*(base**(2+p)))+(lsr_half_limit*base**(x+1-p)))-(grt_half*lsr_half))*2 end p+=1 l+=1 i+=1 end while i < (data[:cmb_length]/2.0).ceil if x%2 == 1 no_limit_matches=get_combinations({:characters=>cap_data[:characters],:cmb_length=>x-(p-1)}) half_point=(((no_limit_matches-get_above_limit({:characters=>cap_data[:characters],:cmb_length=>x-(p-1),:match_limit=>data[:match_limit]})))*base**(x-(p-1)))*2 if data[:cmb_length] == 7 cap_matches+=half_point-((half_point/base)) else half_no_limit=get_combinations(:characters=>cap_data[:characters],:cmb_length=>x-1) half_no_limit_b=get_combinations(:characters=>cap_data[:characters],:cmb_length=>x-2) if data[:cmb_length] >= 9 && data[:extra_chars].nil? puts "!------Approximately less than------!" end cap_matches+=half_point-((half_point/base)) end else no_limit_matches_a=get_combinations({:characters=>cap_data[:characters],:cmb_length=>(x-l)+1}) greater_half=(((no_limit_matches_a-get_above_limit({:characters=>cap_data[:characters],:cmb_length=>(x-l)+1,:match_limit=>data[:match_limit]})))*base**(x-l)) no_limit_matches_b=get_combinations({:characters=>cap_data[:characters],:cmb_length=>(x-l)}) lesser_half=(((no_limit_matches_b-get_above_limit({:characters=>cap_data[:characters],:cmb_length=>(x-l),:match_limit=>data[:match_limit]})))*base**(x-l+1)) if data[:cmb_length] == 10 && data[:extra_chars].nil? puts "!------Approximately less than------!" elsif data[:cmb_length] == 12 && data[:extra_chars].nil? puts "!------Approximately more than------!" end cap_matches+=((greater_half+lesser_half)-((no_limit_matches_a*no_limit_matches_b)-(no_limit_matches_b*base)))*2 end end end if (cap_data[:characters].length <= 2 || data[:cmb_length] >= 13) && data[:extra_chars].nil? puts "!!!----Inaccurate information----!!!" end if !data[:extra_chars].nil? all_chars=data[:characters]+data[:extra_chars] new_cap_matches=cap_limit_matching_l({:cmb_length=>data[:cmb_length],:characters=>all_chars,:match_limit=>data[:match_limit]}) return new_cap_matches-(cap_matches*caps_matched.length) else return cap_matches*caps_matched.length end end |
#create_file(data) ⇒ Object
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/file_size.rb', line 505 def create_file(data) continue=file_info(data) case continue when /(y|Y)/ print "Enter file name:" file_name=gets.chomp @fh=File.open("#{file_name}.txt","a") puts "Creating file" when /(n|N)/ puts "Goodbye" exit else puts "Invalid option" exit end end |
#file_info(data) ⇒ Object
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
# File 'lib/file_size.rb', line 441 def file_info(data) if data[:write_cmbs].nil? poss_combs=get_combinations(data) if !data[:match_limit].nil? if !data[:cap_limit].nil? poss_combs=cap_limit_combs(data) else poss_combs -= get_above_limit(data) end elsif !data[:cap_limit].nil? poss_combs=cap_limit_combs(data) end if @rules poss_combs -=get_rule_size(data) end file_size=get_size({:cmb_length=>data[:cmb_length],:combinations=>poss_combs}) else poss_combs=0 file_size={:bytes=>0,:kilo=>0,:mega=>0,:giga=>0,:tera=>0,:peta=>0} dataB=data.clone cmb_count=data[:write_cmbs].length if !data[:cap_limit].nil? data[:write_cmbs].each do |n| dataB[:cmb_length]=n if data[:cap_limit].nil? current_combs=get_combinations(dataB) if @rules current_combs-=get_rule_size(dataB) end poss_combs +=current_combs unless data[:match_limit].nil? poss_combs -= get_above_limit(dataB) current_combs -= get_above_limit(dataB) end else current_combs=cap_limit_combs(dataB,cmb_count) cmb_count -= 1 poss_combs +=cap_limit_combs(dataB,cmb_count) end get_size({:cmb_length=>n,:combinations=>current_combs})do |sizes| sizes.each do |key,value| file_size[key] +=value end file_size[:bytes]=0 if file_size[:kilo] >= 1 end end end if data[:type] == 'all' puts "All possible combinations #{poss_combs}." else puts "Possible #{data[:type]} combinations #{poss_combs}." end size_string="File size:" file_size.each do |key,value| next if value == 0 size_string += " #{"%.2f"% value}#{key.capitalize}#{'Bytes' if key != :bytes}" end puts size_string puts "Do you wish to continue ?" puts "y|Y n|N" return gets.chomp end |
#get_above_limit(data) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/file_size.rb', line 230 def get_above_limit(data) if data[:extra_chars].nil? base=data[:characters].length else old_base=data[:characters].length previous_matches=0 previous_mult=0 i=0 while i < (data[:cmb_length] -1) if i == (data[:match_limit]-1) previous_matches=old_base old_matches=previous_matches elsif i == data[:match_limit] previous_matches=previous_matches+(1*(old_base-1)) previous_mult=1 else temp_mult=previous_matches previous_matches=(old_base**(i-1)+previous_mult*(old_base-1))+(previous_matches*(old_base-1)) previous_mult=temp_mult end i+=1 end old_matches=old_base*previous_matches if data[:cmb_length] != (data[:match_limit]+1) base=data[:characters].length+data[:extra_chars].length end previous_matches=0 previous_mult=0 i=1 while i < (data[:cmb_length] -1) if i == (data[:match_limit]-1) previous_matches=base return previous_matches if data[:cmb_length] == (data[:match_limit]+i) && data[:extra_chars].nil? elsif i == data[:match_limit] previous_matches=previous_matches+(1*(base-1)) previous_mult=1 else temp_mult=previous_matches previous_matches=(base**(i-1))+(previous_mult*(base-1))+(previous_matches*(base-1)) previous_mult=temp_mult end i+=1 end unless data[:extra_chars].nil? return base-old_base if data[:cmb_length] == (data[:match_limit]+1) return (base*previous_matches)-old_matches else return base*previous_matches end end |
#get_combinations(data) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/file_size.rb', line 304 def get_combinations(data) if data[:extra_chars].nil? base=data[:characters].length else old_base=data[:characters].length old_combinations=old_base**data[:cmb_length] x=0 post_matches=0 while x < data[:cmb_length]-1 if x == 0 post_matches=old_base elsif x == 1 post_matches=(old_base**x)+(1*(old_base-1)) else post_matches=(old_base**x)+(post_matches*(old_base-1)) end x+=1 end old_matches=old_base*post_matches base=data[:characters].length+data[:extra_chars].length end previous_matches=0 i=0 while i < data[:cmb_length]-1 if i==0 previous_matches=base elsif i== 1 previous_matches=(base**i)+(1*(base-1)) else previous_matches=(base**i)+(previous_matches*(base-1)) end i+=1 end matches=base*previous_matches if data[:type]=='unique' possible_combinations=base**data[:cmb_length] if data[:uniqueness_type].nil? return (possible_combinations-matches)-(old_combinations-old_matches) if !data[:extra_chars].nil? return possible_combinations-matches else single_combs=get_single_combs(data) if data[:uniqueness_type]=="single" return single_combs elsif data[:uniqueness_type]=="repeat" return ((possible_combinations-matches)-(old_combinations-old_matches))-single_combs if !data[:extra_chars].nil? return (possible_combinations-matches)-single_combs end end elsif data[:type]=='matching' return matches-old_matches if !data[:extra_chars].nil? return matches else if data[:extra_chars] remainder = (base ** data[:cmb_length]).-(old_base**data[:cmb_length]) return remainder else return base ** data[:cmb_length] end end return 0 end |
#get_rule_size(data) ⇒ Object
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/file_size.rb', line 411 def get_rule_size(data) characters=data[:characters].join() digits=characters.scan(/[0-9]/) if data[:cap_limit] letters=characters.scan(/[a-z]/i) else letters=characters.scan(/[A-Z]/i) end combinations=0 if data[:extra_chars].nil? if letters.length > 0 combinations+=get_strictness_one(letters,digits,data[:cmb_length]) end if @strictness >= 2 combinations +=get_combinations({:cmb_length=>data[:cmb_length], :characters=>digits}) end if @strictness == 3 combinations +=get_combinations({:cmb_length=>data[:cmb_length], :characters=>letters,:extra_chars=>data[:extra_chars]}) end end return combinations end |
#get_single_combs(data) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/file_size.rb', line 285 def get_single_combs(data) i=0 single_combinations=0 extra_singe=0 extra_chars=data[:characters].length+data[:extra_chars].length if !data[:extra_chars].nil? while i < data[:cmb_length]-1 if i == 0 single_combinations=(data[:characters].length-1)*data[:characters].length extra_singe=extra_chars*(extra_chars-1) if !extra_chars.nil? else single_combinations=single_combinations*(data[:characters].length-(i+1)) extra_singe=extra_singe*(extra_chars-(i+1)) if !extra_chars.nil? end i+=1 end return extra_singe - single_combinations if !data[:extra_chars].nil? return single_combinations end |
#get_size(data) {|return_sizes| ... } ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/file_size.rb', line 3 def get_size(data) bytes=data[:combinations]*(data[:cmb_length]+1) kilo_bytes=bytes/1000.0 mega_bytes=kilo_bytes/1000.0 giga_bytes=mega_bytes/1000.0 tera_bytes=giga_bytes/1000.0 peta_bytes=tera_bytes/1000.0 all_sizes= {:bytes=>bytes,:kilo=>kilo_bytes,:mega=>mega_bytes,:giga=>giga_bytes,:tera=>tera_bytes,:peta=>peta_bytes} return_sizes= {} all_sizes.each do |key,value| if kilo_bytes < 1 return_sizes[key]=value break end return_sizes[key]=value if value > 0.01 && key != :bytes end yield(return_sizes) if block_given? return return_sizes end |
#get_strictness_one(letters, digits, cmb_length) ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/file_size.rb', line 368 def get_strictness_one(letters,digits,cmb_length) combinations=0 if cmb_length % 2 != 0 ceiling=(cmb_length/2.0).ceil floor=(cmb_length/2.0).floor end if cmb_length == 6 combinations+=get_combinations({:characters=>digits,:cmb_length=>(cmb_length/2)})*2 combinations+=get_combinations({:characters=>letters,:cmb_length=>(cmb_length/2)})*2 elsif cmb_length == 7 combinations+=get_combinations({:characters=>digits,:cmb_length=>floor})*2 combinations+=get_combinations({:characters=>letters,:cmb_length=>ceiling})*2 elsif cmb_length == 8 combinations+=get_combinations({:characters=>digits,:cmb_length=>(cmb_length/2)})*2 combinations+=get_combinations({:characters=>letters,:cmb_length=>(cmb_length/2)})*2 elsif cmb_length == 9 combinations+=get_combinations({:characters=>digits,:cmb_length=>floor})*2 combinations+=get_combinations({:characters=>letters,:cmb_length=>ceiling})*2 elsif cmb_length == 10 combinations+=get_combinations({:characters=>digits,:cmb_length=>(cmb_length/2)})*2 combinations+=get_combinations({:characters=>letters,:cmb_length=>(cmb_length/2)})*2 elsif cmb_length == 11 combinations+=get_combinations({:characters=>digits,:cmb_length=>floor})*2 combinations+=get_combinations({:characters=>letters,:cmb_length=>ceiling})*2 elsif cmb_length == 12 combinations+=get_combinations({:characters=>digits,:cmb_length=>(cmb_length/2)})*2 combinations+=get_combinations({:characters=>letters,:cmb_length=>(cmb_length/2)})*2 elsif cmb_length == 13 combinations+=get_combinations({:characters=>digits,:cmb_length=>floor})*2 combinations+=get_combinations({:characters=>letters,:cmb_length=>ceiling})*2 elsif cmb_length == 14 combinations+=get_combinations({:characters=>digits,:cmb_length=>(cmb_length/2)})*2 combinations+=get_combinations({:characters=>letters,:cmb_length=>(cmb_length/2)})*2 elsif cmb_length == 15 combinations+=get_combinations({:characters=>digits,:cmb_length=>floor})*2 combinations+=get_combinations({:characters=>letters,:cmb_length=>ceiling})*2 end return combinations end |
#unique_cap_limit(data) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/file_size.rb', line 162 def unique_cap_limit(data) cap_data={} total_chars=data[:characters].join() caps_matched= total_chars.scan(/[A-Z]/) cap_data[:characters]=[] data[:characters].each do |char| next if caps_matched.include?(char) cap_data[:characters].push(char) end base=cap_data[:characters].length unique_cap_limit=0 if data[:uniqueness_type] == 'single' if data[:cmb_length] == 3 unique_cap_limit=(((base**2)-(base))*3) else previous_single=get_single_combs(:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-1,:type=>"unique") unique_cap_limit=previous_single*data[:cmb_length] end else case data[:cmb_length] when 3 if data[:uniqueness_type].nil? unique_cap_limit=((base**2)*3)-(base*2) else unique_cap_limit=((base**2)*3)-(base*2)-(((base**2)-(base))*3) end when 4 previous_unique=get_combinations(:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-1,:type=>"unique") if data[:uniqueness_type].nil? unique_cap_limit=(previous_unique*2)+((((base**2)-base)*base)*2) else previous_single=get_single_combs(:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-1,:type=>"unique") unique_cap_limit=(previous_unique*2)+((((base**2)-base)*base)*2)-(previous_single*data[:cmb_length]) end else previous_unique_a=get_combinations(:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-1,:type=>"unique") previous_unique_b=get_combinations(:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-2,:type=>"unique") if data[:uniqueness_type].nil? unique_cap_limit=(previous_unique_a*2)+((previous_unique_b*base)*(data[:cmb_length]-2)) else previous_single=get_single_combs(:characters=>cap_data[:characters],:cmb_length=>data[:cmb_length]-1,:type=>"unique") unique_cap_limit=(previous_unique_a*2)+((previous_unique_b*base)*(data[:cmb_length]-2))-(previous_single*data[:cmb_length]) end end end if !data[:extra_chars].nil? all_chars=data[:characters]+data[:extra_chars] new_unique_cap_limit=unique_cap_limit({:cmb_length=>data[:cmb_length],:characters=>all_chars,:uniqueness_type=>data[:uniqueness_type]}) return new_unique_cap_limit-(unique_cap_limit*caps_matched.length) else return unique_cap_limit*caps_matched.length end end |