| 1 |
package Rsa |
| 2 |
|
| 3 |
import ( |
| 4 |
"io/ioutil" |
| 5 |
"math/big" |
| 6 |
"testing" |
| 7 |
. "gopkg.in/check.v1" |
| 8 |
) |
| 9 |
|
| 10 |
func Test(t *testing.T) { TestingT(t) } |
| 11 |
type TestSuite struct{} |
| 12 |
var _ = Suite(&TestSuite{}) |
| 13 |
|
| 14 |
|
| 15 |
func (s *TestSuite) TestNewPrivateRsaKeyPKCS1(c *C) { |
| 16 |
|
| 17 |
//given |
| 18 |
keyBytes, _ := ioutil.ReadFile("priv.pem") |
| 19 |
|
| 20 |
//when |
| 21 |
test,e := ReadPrivate(keyBytes) |
| 22 |
|
| 23 |
//then |
| 24 |
c.Assert(e, IsNil) |
| 25 |
c.Assert(test.D, DeepEquals, bigInt("124664500442337916425629948081980373708538939606058968438393904444884872144817323585651521088406960965803456218656807473171367526874204593887158860695954252405916280866383288176587587469870736553178754458686625418367566925639753001190058223163696966510495553421774149906056482201169970508069587759758018186673")) |
| 26 |
c.Assert(test.Primes, HasLen, 2) |
| 27 |
c.Assert(test.Primes[0], DeepEquals, bigInt("12153319500662601635346432957857245055929638609873673015885135318360261418965835817520733410832489021447922460160007366037402643276624674886502627435389593")) |
| 28 |
c.Assert(test.Primes[1], DeepEquals, bigInt("10891140096045830766355607974974880425030121636560867106153739255802668290122259699296919947637286459736524694263050343032461619592457541146877906031793883")) |
| 29 |
c.Assert(test.E, Equals, 65537) |
| 30 |
c.Assert(test.N, DeepEquals, bigInt("132363505313722155184876628715249052276006747427497555521215412160460427148722445294983292629743653314149228192824806664548107146354876411009845771623017501212568714555984634301944063168503260813720165931647377932654326833738173931173570212088015547040743996672392373035728084429156775233489529905624779259619")) |
| 31 |
} |
| 32 |
|
| 33 |
func (s *TestSuite) TestNewPrivateRsaKeyPKCS8(c *C) { |
| 34 |
|
| 35 |
//given |
| 36 |
keyBytes, _ := ioutil.ReadFile("priv.pem") |
| 37 |
|
| 38 |
//when |
| 39 |
test,e := ReadPrivate(keyBytes) |
| 40 |
|
| 41 |
//then |
| 42 |
c.Assert(e, IsNil) |
| 43 |
c.Assert(test.D, DeepEquals, bigInt("124664500442337916425629948081980373708538939606058968438393904444884872144817323585651521088406960965803456218656807473171367526874204593887158860695954252405916280866383288176587587469870736553178754458686625418367566925639753001190058223163696966510495553421774149906056482201169970508069587759758018186673")) |
| 44 |
c.Assert(test.Primes, HasLen, 2) |
| 45 |
c.Assert(test.Primes[0], DeepEquals, bigInt("12153319500662601635346432957857245055929638609873673015885135318360261418965835817520733410832489021447922460160007366037402643276624674886502627435389593")) |
| 46 |
c.Assert(test.Primes[1], DeepEquals, bigInt("10891140096045830766355607974974880425030121636560867106153739255802668290122259699296919947637286459736524694263050343032461619592457541146877906031793883")) |
| 47 |
c.Assert(test.E, Equals, 65537) |
| 48 |
c.Assert(test.N, DeepEquals, bigInt("132363505313722155184876628715249052276006747427497555521215412160460427148722445294983292629743653314149228192824806664548107146354876411009845771623017501212568714555984634301944063168503260813720165931647377932654326833738173931173570212088015547040743996672392373035728084429156775233489529905624779259619")) |
| 49 |
} |
| 50 |
|
| 51 |
func (s *TestSuite) TestNewPublicRsaKeyPKCS1(c *C) { |
| 52 |
|
| 53 |
//given |
| 54 |
keyBytes, _ := ioutil.ReadFile("pub.pem") |
| 55 |
|
| 56 |
//when |
| 57 |
test,e := ReadPublic(keyBytes) |
| 58 |
|
| 59 |
//then |
| 60 |
c.Assert(e, IsNil) |
| 61 |
c.Assert(test.E, Equals, 65537) |
| 62 |
c.Assert(test.N, DeepEquals, bigInt("132363505313722155184876628715249052276006747427497555521215412160460427148722445294983292629743653314149228192824806664548107146354876411009845771623017501212568714555984634301944063168503260813720165931647377932654326833738173931173570212088015547040743996672392373035728084429156775233489529905624779259619")) |
| 63 |
} |
| 64 |
|
| 65 |
func (s *TestSuite) TestNewPublicRsaKeyPKIX(c *C) { |
| 66 |
|
| 67 |
//given |
| 68 |
keyBytes, _ := ioutil.ReadFile("pub.key") |
| 69 |
|
| 70 |
//when |
| 71 |
test,e := ReadPublic(keyBytes) |
| 72 |
|
| 73 |
//then |
| 74 |
c.Assert(e, IsNil) |
| 75 |
c.Assert(test.E, Equals, 65537) |
| 76 |
c.Assert(test.N, DeepEquals, bigInt("132363505313722155184876628715249052276006747427497555521215412160460427148722445294983292629743653314149228192824806664548107146354876411009845771623017501212568714555984634301944063168503260813720165931647377932654326833738173931173570212088015547040743996672392373035728084429156775233489529905624779259619")) |
| 77 |
} |
| 78 |
|
| 79 |
//utils |
| 80 |
func bigInt(value string) *big.Int { |
| 81 |
i:=new (big.Int) |
| 82 |
i.SetString(value,10) |
| 83 |
|
| 84 |
return i |
| 85 |
} |