Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with Message initialization #12

Closed
mobileapps23 opened this issue Jan 8, 2020 · 8 comments
Closed

Error with Message initialization #12

mobileapps23 opened this issue Jan 8, 2020 · 8 comments

Comments

@mobileapps23
Copy link

Hello!
Your lib perfectly matches to my goals, but I have trouble with Message from your example:
let message = Message(hex: "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89" An error occurred: Cannot invoke initializer for type 'Message' with an argument list of type '(hex: String)'

How could I init Message with some String?

@mobileapps23 mobileapps23 changed the title Error with Message initiaization Error with Message initialization Jan 8, 2020
@Sajjon
Copy link
Owner

Sajjon commented Feb 12, 2020

@mobileapps23 So sorry for slow response, saw this just now... But I think I talked to you on StackOverflow?

Hey what kind of data do you wanna sign/verify?

There are five different initializers of Message. You ought to find a suitable one. Remember that the data should be hashed before signing. Which is why I've added convenience inits such:

init<H>(unhashed: Data, hashFunction: H) where H: HashFunction
    init?<H>(
        unhashed: String,
        encoding: String.Encoding = .default,
        hashFunction: H
    ) where H: HashFunction

and

init?<H>(hashedHex: HexString, hashedBy hashFunction: H) where H: HashFunction

@narayanprusty
Copy link

@Sajjon still same issue. Can you provide code example?

@Sajjon
Copy link
Owner

Sajjon commented Feb 16, 2020

@narayanprusty look at the unit tests, e.g.: https://github.com/Sajjon/EllipticCurveKit/blob/master/Tests/EllipticCurveKitTests/CurveTests/ECDSATests.swift

which also defines convenience inits:


// MARK: Message init
extension Message {
    init(message: String) {
        self.init(unhashed: message, encoding: .default, hashFunction: SHA256())!
    }

    init(hex: String) {
        self.init(hashedHex: hex, hashedBy: SHA256())!
    }
}

@luis7lucho
Copy link

Hi, quick one, is there a way to sign without hashing the message?
I am trying to replicate org.web3j.crypto framework which has the option to sign without message hashing (and you also hash with SHA3 if you wanted to) but I can't find a way to sign without message hashing here.
Thanks

@Sajjon
Copy link
Owner

Sajjon commented Feb 25, 2020

@luis7lucho Sounds like you don't really know what you are doing? We ALWAYS hash stuff in crypto. Always. You cannot sign a text document being a couple of hundreds of KB or even MB. That makes no sense. We ALWAYS hash the thing we wanna sign.

So either you already have a hash, then you can you the init in init:hashedHex:hashedBy where whatever you pass into hashedBy is ignored. I'm requiring you to pass a HashFunction (via hashedBy) to force you into thinking about how the data you pass is hashed, explicitly highlighting that everything always should be hashed.

You can also use init:rawData, which is will be the exact same thing as init:hashedHex:hashedBy. But I disencourage that.

So sounds like you don't really know what you are signing...? Which really is what you should sort ought first. What are you signing? When you know what you are signing, why not just use the initializers I encourage you to use, e.g. init:unhashedData:hashFunction which does the hashing for you and where you specify which hash funtion to use.

Makes sense?

@luis7lucho
Copy link

Hi Sajjon, thanks for the reply.
I was trying to replicate a code from the web3j.crypto library from Android wich lets you create a signature without hashing the message (it has a Bool parameter which you can pass false if you do not wish to hash the message). I am not arguing wether you SHOULD or SHOULD NOT, I agree you always should, but for this particuar scenario I needed that. I got around it eventually anyway, but thanks for your reply :)

I have noticed something on your latests commits, the test and the method to generate a signature takes quite a bit of time, a bit when generating the public key but mostly generating the signature, I have put this on a background thread but wanted to know if there is a way around that?
Thanks

@Sajjon
Copy link
Owner

Sajjon commented Feb 25, 2020

@luis7lucho I will be closing this issue then, since neither @mobileapps23 nor @narayanprusty have replied.

@luis7lucho regarding performance, since the project is now a SPM package, the tests are no longer run with optimization flag. I have not found a way of using optimization with tests yet, I created a this StackOverflow question, but no reply yet - I'm pretty sure it is not possible yet... Hopefully soon!

@Sajjon Sajjon closed this as completed Feb 25, 2020
@Sajjon
Copy link
Owner

Sajjon commented Feb 25, 2020

@luis7lucho please upvote the SO question, so maybe someone posts an answer, thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants