Creating signed rpm in Linux

Recently someone came up to me with this query – How to generate a signed rpm in linux?

After building your RPM package, it’s a good idea to sign the package with your own GPG Key to make sure the package is authentic.  Well, before you could sign an rpm package at build time, you must create a gpg key and a passphrase
Below are steps for the same –
  1. Generate a signature key – 
———–clip———
[xxx@yyy]$ gpg –gen-key
gpg (GnuPG) 1.4.5; Copyright (C) 2006 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.
Please select what kind of key you want:
   (1) DSA and Elgamal (default)
   (2) DSA (sign only)
   (5) RSA (sign only)
Your selection? 1
DSA keypair will have 1024 bits.
ELG-E keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 2048
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 0
Key does not expire at all
Is this correct? (y/N) y
You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
    “Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>”
Real name: john
Email address: john@testmail.com
Comment: No Comments
You selected this USER-ID:
    “john(No Comments) <john@testmail.com>”
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key.
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
++++++++++++++++++++.++++++++++++++++++++++++++++++…++++++++++.++++++++++++++++++++++++++++++.+++++++++++++++.++++++++++++++++++++++++++++++………………………………………………………>.+++++…………………………………………………………….+++++
Not enough random bytes available.  Please do some other work to give
the OS a chance to collect more entropy! (Need 283 more bytes)
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++.+++++++++++++++++++++++++++++++++++….++++++++++.+++++.+++++++++++++++++++++++++..+++++>+++++..+++++>..+++++…….>+++++………………………………………………………………………………………….+++++^^^
gpg: key DB17E9EE marked as ultimately trusted
public and secret key created and signed.
gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   1024D/DB17E9EE 2012-07-19
      Key fingerprint = 22E1 4A80 9899 0A27 4BD8  3200 37C4 857A DB17 E9EE
uid                  john(No Comments) <john@testmail.com>
sub   2048g/E7FFDEEF 2012-07-19
———–clap———
2.  Confirm that the above created signatures are present in the signature repository –
———–clip———-
[xxx@yyy]$ gpg –list-sigs
/home/xxx/.gnupg/pubring.gpg
———————————
pub   1024D/DB17E9EE 2012-07-19
uid                  xxx(No Comments) <john@testmail.com>
sig 3        DB17E9EE 2012-07-19  john(No Comments) <john@testmail.com>
sub   2048g/E7FFDEEF 2012-07-19
sig          DB17E9EE 2012-07-19  john(No Comments) <john@testmail.com>
———–clap———-
3. Create a file “.rpmmacros” in your home directory and add the below lines to the same ( I’ve given “john” to “%_gpg_name” as this is the real name of the signature owner that i have provided in Step 1) –
——-clip——-
%_signature gpg
%_gpg_name john
%_gpgbin /usr/bin/gpg
——-clap——-
4. Now, you’re all  ready to sign your RPM package –
——-clip——-
[xxx@yyy]$ rpm –addsign XXX-YYY-ZZZ-2.0-5.WW.i386.rpm                               // rpm name is just an example
Enter pass phrase:                   // Here provide the pass phrase as "test" because this is what I have provided 
                                     // for the signature owner "John" in Step 1
Pass phrase is good.
XXX-YYY-ZZZ-2.0-5.WW.i386.rpm:
——-clap——-
You can verify your signature on the RPM  using the command:

rpm –checksig XXX-YYY-ZZZ-2.0-5.WW.i386.rpm     // rpm name is just an example