Wish list for 2.0

Discussion in 'Ruby' started by STEPHEN BECKER I V, Sep 28, 2004.

  1. STEPHEN BECKER I V, Sep 28, 2004
    #1
    1. Advertisements

  2. 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.
     
    Yukihiro Matsumoto, Sep 28, 2004
    #2
    1. Advertisements

  3. does that work for end lines in the middle of a string? or a tab? I
    mean all white space..
     
    STEPHEN BECKER I V, Sep 28, 2004
    #3
  4. STEPHEN BECKER I V

    Phlip Guest

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

    (I clicked looking for a wish list. I can't think of anything either...)
     
    Phlip, Sep 28, 2004
    #4
  5. 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.
     
    Yukihiro Matsumoto, Sep 28, 2004
    #5
  6. Perhaps String#delete could take a regex as well?

    str.delete /\s/

    Gavin
     
    Gavin Sinclair, Sep 28, 2004
    #6
  7. 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.
     
    Yukihiro Matsumoto, Sep 28, 2004
    #7
  8. A deletew or rmwhite where it deletes all the white space from a string
    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.
     
    Brian Candler, Sep 28, 2004
    #8
  9. That makes sense. Might as well implement it like that so it can be
    documented as such.

    Gavin
     
    Gavin Sinclair, Sep 28, 2004
    #9

  10. 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 :)
     
    Simon Strandgaard, Sep 28, 2004
    #10
  11. how about isprime, nextprime, preprime?
     
    STEPHEN BECKER I V, Sep 30, 2004
    #11
  12. STEPHEN BECKER I V

    Markus Guest

    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
     
    Markus, Sep 30, 2004
    #12
  13. 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
     
    Brian Schröder, Sep 30, 2004
    #13
  14. It's not something one would use extremely often, now would it?
    Anyway, aren't those already in a math module or something?
     
    Vincent Foley, Oct 1, 2004
    #14
  15. STEPHEN BECKER I V

    vruz Guest

    vruz, Oct 1, 2004
    #15
  16. 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.
     
    STEPHEN BECKER I V, Oct 2, 2004
    #16
  17. STEPHEN BECKER I V

    vruz Guest

    no they are not. They are in the Mbignum but that is not standard i

    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
     
    vruz, Oct 2, 2004
    #17
  18. --/04w6evG8XlLl3ft
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline

    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--
     
    Brian Candler, Oct 2, 2004
    #18
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.