Wish list for 2.0

Y

Yukihiro Matsumoto

Hi,

In message "Re: Wish list for 2.0"

|A deletew or rmwhite where it deletes all the white space from a string
| If you can use delete(/s) or something PLEASE let me know! :)

str.delete(" ")

matz.
 
S

STEPHEN BECKER I V

does that work for end lines in the middle of a string? or a tab? I
mean all white space..
 
P

Phlip

STEPHEN said:
does that work for end lines in the middle of a string? or a tab? I
mean all white space..

What's wrong with str.gsub!(/\s/, ''), or similar ?

(I clicked looking for a wish list. I can't think of anything either...)
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Wish list for 2.0"

|does that work for end lines in the middle of a string? or a tab? I
|mean all white space..

str.delete(" \n\t")

if you wish.

matz.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Wish list for 2.0"

|Perhaps String#delete could take a regex as well?
|
| str.delete /\s/

Interesting idea. What if pattern match overwrap, e.g.

str = "aabab"
str.delete(/a./)

? Should it work as str.gsub(pat, "") ?

matz.
 
B

Brian Candler

A deletew or rmwhite where it deletes all the white space from a string
If you can use delete(/s) or something PLEASE let me know! :)

str = " abc def \n ghi jkl "
str.gsub!(/\s+/,'')
p str
# prints "abcdefghijkl"

You can define this as your own String#deletew method easily enough if you
need to use it over and over again:

class String
def deletew!
gsub!(/\s+/,'')
end
def deletew
dup.deletew!
end
end

Regards,

Brian.
 
S

Simon Strandgaard

That makes sense. Might as well implement it like that so it can be
documented as such.


Maybe model the behavior after #scan, so that it deletes the matches that scan
would have found. This way we can get very flexible deletion in case
captures are being used.

I propose #delete to erase those fragments that #scan matches.

irb(main):011:0> 'a12bc34de56fg78hi'.scan(/(\d).{2,}?(\d)/)
=> [["1", "3"], ["4", "5"], ["6", "7"]]
irb(main):012:0> 'a12bc34de56fg78hi'.scan(/\d.{2,}?\d/)
=> ["12bc3", "4de5", "6fg7"]
irb(main):013:0>


Lets change above 2 scan statements into delete

irb(main):011:0> 'a12bc34de56fg78hi'.delete(/(\d).{2,}?(\d)/)
=> "a2bcdefg8hi"
irb(main):012:0> 'a12bc34de56fg78hi'.delete(/\d.{2,}?\d/)
=> "a8hi"
irb(main):013:0>


Ok.. this is not the most obvious example.. but I hope you get the point.
Otherwise please ask :)
 
M

Markus

how about isprime, nextprime, preprime?

Heck, why not a Numeric#factors (it should work on Bignums too)...

Or how about just something that gives you the zeros of the zeta
function?

Or...

-- MarkusQ
 
B

Brian Schröder

Markus said:
Heck, why not a Numeric#factors (it should work on Bignums too)...

thats easy. Just guess the correct factorization and validate. Only need
to find my non-deterministic turing machine. ... I must have put it
somewhere ....

Brian
 
V

Vincent Foley

STEPHEN BECKER I V said:
how about isprime, nextprime, preprime?

It's not something one would use extremely often, now would it?
Anyway, aren't those already in a math module or something?
 
S

STEPHEN BECKER I V

no they are not. They are in the Mbignum but that is not standard i
guess. I think it would be useful, I think if you had the RSA key
producers built in it would help ruby become more popular with
security program. or at least crypto stuff.
 
V

vruz

no they are not. They are in the Mbignum but that is not standard i
guess. I think it would be useful, I think if you had the RSA key
producers built in it would help ruby become more popular with
security program. or at least crypto stuff.


You are right, MBignum is not standard, still very useful for the
purposes you've mentioned.
Someone proposed to make it standard some time ago but not
sure how that turned how at last.
In any case, the best answer can be provided by the author himself.

cheers,
vruz
 
B

Brian Candler

--/04w6evG8XlLl3ft
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

no they are not. They are in the Mbignum but that is not standard i
guess. I think it would be useful, I think if you had the RSA key
producers built in it would help ruby become more popular with
security program. or at least crypto stuff.

Ruby 1.8.x has openssl built in:

require 'openssl'
k = OpenSSL::pKey::RSA.new(256) # generate 256 bit RSA private key
puts k.p, k.q, k.n # two primes and their product
p k.p * k.q == k.n # true

OpenSSL's bignum library is wrapped which includes a number of important
mathematical operations. See
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/3367 for an
example of them in action, generating RSA private key parameters from p, q
and e.

The attached Ruby snippets will:
1. take a PEM public key file and extract n and e
2. take n, e and factors of n (p, q) and create a PEM private key file

All you then need is something like mpqs4linux to break RSA keys. You can do
a 256-bit key in about 45 minutes on a PIII-933MHz without needing to
understand any maths at all :)

Regards,

Brian.

--/04w6evG8XlLl3ft
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="crackrsa1.rb"

#!/usr/local/bin/ruby -w

require 'openssl'

# Input: a PEM-encoded public key
# Output: a large number to factorise
# an exponent

# This is very convenient in Ruby because (a) it already has support for
# arbitary large numbers, and (b) OpenSSL support is included in the
# ruby 1.8 base package

a = OpenSSL::pKey::RSA.new($stdin.read)
puts a.n
puts a.e

--/04w6evG8XlLl3ft
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="crackrsa2.rb"

#!/usr/local/bin/ruby -w

require 'openssl'

# Input: a large number n
# an exponent
# factor p of n
# factor q of n (n = p * q)
# Output: a PEM-encoded private key

# This is a bit more inconvenient because Ruby OpenSSL doesn't have a
# constructor for RSA keys from parameters, so we write one here

module OpenSSL
module PKey
class RSA

# Construct an RSA key from parameters.
# For private keys, if d is nil then it is calculated from p and q

def self.new_from_parameters(n, e, d=nil, p=nil, q=nil)
a = self.new # self.new(64) for ruby < 1.8.2
a.n = n # converted to OpenSSL::BN automatically
a.e = e
if p and q
a.p = p
a.q = q
raise "n != p * q" unless a.n == a.p * a.q
a.d = d || a.e.mod_inverse((a.p-1)*(a.q-1))
a.dmp1 = a.d % (a.p-1)
a.dmq1 = a.d % (a.q-1)
a.iqmp = a.q.mod_inverse(a.p)
else
a.d = d
a.p = nil
a.q = nil
end
a
end
end
end
end

n = gets.to_i
e = gets.to_i
p = gets.to_i
q = gets.to_i
p,q = q,p if p < q

puts OpenSSL::pKey::RSA.new_from_parameters(n, e, nil, p, q)

--/04w6evG8XlLl3ft--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top