How to use the sign function from tweetnacl
Find comprehensive JavaScript tweetnacl.sign code examples handpicked from public code repositorys.
tweetnacl.sign is a function that generates digital signatures using the Ed25519 public-key signature system, allowing for secure message authentication and verification.
GitHub: rashmiranjanbhoi/RepoC
310 311 312 313 314 315 316 317 318 319 320
var S = pubKey._pub.multiply(this._priv); return (Buffer.from(S.getX().toBigInteger().toByteArray())); }; function generateED25519() { var pair = nacl.sign.keyPair(); var priv = Buffer.from(pair.secretKey); var pub = Buffer.from(pair.publicKey); assert.strictEqual(priv.length, 64); assert.strictEqual(pub.length, 32);
301 302 303 304 305 306 307 308 309 310 311 312 313
} function calculateED25519Public(k) { assert.buffer(k); var kp = nacl.sign.keyPair.fromSeed(new Uint8Array(k)); return (Buffer.from(kp.publicKey)); } function calculateX25519Public(k) {
+ 7 other calls in file
How does tweetnacl.sign work?
tweetnacl.sign
is a function provided by the TweetNaCl library that generates digital signatures using the Ed25519 public-key signature system.
When tweetnacl.sign
is called with a message and a secret key, it performs the following steps to generate a signature:
- The secret key is used to generate a corresponding public key using the Ed25519 public-key signature system.
- The message is hashed using SHA-512, a cryptographic hash function that produces a fixed-size output from an arbitrary-sized input.
- The hashed message is then signed using the secret key and the Ed25519 signature system, producing a 64-byte signature.
- The signature is then appended to the original message to produce the signed message.
Once a message has been signed using tweetnacl.sign
, the signature can be used to authenticate the message and verify its integrity. To do this, the verifier uses the public key corresponding to the secret key used to sign the message to verify the signature.
tweetnacl.sign
can be useful for applications that require secure message authentication and verification, such as encrypted messaging systems or secure file transfer protocols.
105 106 107 108 109 110 111 112 113 114
} else if (this.type === 'curve25519' && newType === 'ed25519') { priv = this.part.k.data; if (priv[0] === 0x00) priv = priv.slice(1); pair = nacl.sign.keyPair.fromSeed(new Uint8Array(priv)); pub = Buffer.from(pair.publicKey); return (new PrivateKey({ type: 'ed25519',
+ 7 other calls in file
GitHub: onuruci/solauth
127 128 129 130 131 132 133 134 135 136
console.log(req.body); const publicKey = req.body.publicKey; const signature = req.body.signature; const signMessage = "sign"; const verified = nacl.sign.detached.verify( new TextEncoder().encode(signMessage), bs58.decode(signature), bs58.decode(publicKey) );
+ 13 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
const nacl = require("tweetnacl"); // Generate a random 32-byte secret key const secretKey = nacl.randomBytes(32); // Generate the corresponding public key const publicKey = nacl.sign.keyPair.fromSecretKey(secretKey).publicKey; // Create a message to sign const message = "Hello, World!"; // Hash the message using SHA-512 const hashedMessage = nacl.hash(new TextEncoder().encode(message)); // Sign the hashed message using the secret key and the Ed25519 signature system const signature = nacl.sign.detached(hashedMessage, secretKey); // Append the signature to the original message to produce the signed message const signedMessage = message + ":" + nacl.util.encodeBase64(signature); // Verify the signed message using the public key and the Ed25519 signature system const [unsignedMessage, decodedSignature] = signedMessage.split(":"); const isVerified = nacl.sign.detached.verify( nacl.hash(new TextEncoder().encode(unsignedMessage)), nacl.util.decodeBase64(decodedSignature), publicKey ); console.log("Message:", message); console.log("Signed message:", signedMessage); console.log("Verified:", isVerified);
In this example, we're using the tweetnacl library to generate a random 32-byte secret key and the corresponding public key using the Ed25519 signature system. We're then creating a message to sign, hashing the message using SHA-512, and signing the hashed message using the secret key and the Ed25519 signature system. We're appending the signature to the original message to produce the signed message, and then verifying the signed message using the public key and the Ed25519 signature system. When we run this code, we'll get output that looks something like this: ruby Copy code
139 140 141 142 143 144 145 146 147 148
this.message, ]); } verifySignature(signature, publicKey) { return nacl.sign.detached.verify( this.serialize(), signature, publicKey.toBuffer() );
+ 13 other calls in file
GitHub: OlehPotap/nature-ui-demo
17 18 19 20 21 22 23 24 25 26
if (!bip39.validateMnemonic(mnemonic)) { return false; } const seed = bip39.mnemonicToSeedSync(mnemonic); const derivedKey = slip10.derivePath(bip32Path, seed.toString("hex")); const keypair = tweetnacl.sign.keyPair.fromSeed( Buffer.from(derivedKey.key, "hex") ); let publicKey = `NATURE${Buffer.from(keypair.publicKey).toString("hex")}`; let privateKey = Buffer.from(keypair.secretKey)
+ 19 other calls in file
21 22 23 24 25 26 27 28 29 30
if (!bip39.validateMnemonic(mnemonic)) { return false; } const seed = bip39.mnemonicToSeedSync(mnemonic); const derivedKey = slip10.derivePath(bip32Path, seed.toString('hex')); const keypair = tweetnacl.sign.keyPair.fromSeed(Buffer.from(derivedKey.key, 'hex')); let publicKey = `NATURE${Buffer.from(keypair.publicKey).toString('hex')}`; let privateKey = Buffer.from(keypair.secretKey).subarray(0, 32).toString('hex'); return {
+ 15 other calls in file
tweetnacl.box is the most popular function in tweetnacl (320 examples)