How to get the key from a keygen?

Asked

Viewed 66 times

1

I have an HTML document with a form and I want to send a random key to the server. To do this, I’m using the element <keygen>.

Before the data is sent, I need to get the value of this element <keygen> to store the generated key locally. I tried the following code:

function getKey() {
    const keygen = document.getElementsByTagName("keygen")[0];
    sessionStorage.token = keygen.accessKey;
}

document.onsubmit = getKey;

The problem is that it always returns an empty string instead of the generated key. What should I do to get the key before sending the form?

  • 3

    <keygen> is an obsolete feature of HTML5. I would question its use.

  • I just want to make it clear that it was not I who gave the negative in the question. Although it is an obsolete element, I don’t think the question is bad... I’d like to know why the negative.

  • 1

    I also did not see the need for the negative.

  • 1

    The attribute you were looking for is challenge but I’m sorry to inform you that the tag <keygen> no longer works on any browser the API has been removed: Reference https://caniuse.com/#search=keygen

  • So what should I use to generate random keys? I don’t want to create an entire function just for that ;/

  • 1

    @Jeanextreme002 This helps Window.crypto ?

  • Use <keygen> will return [Object Htmlunknownelement] in localStorage or sessionStorage.

  • @Jeanextreme002, I think it’s worth [Edit] the question to tell you what you really need to do. If you want to generate random keys, <keygen> - deprecated tag -, does not seem to be a requirement, right?

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.