| 1 | package torutilscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/ |
| 2 | |
| 3 | import ( |
| 4 | "crypto/randscip-go gomod github.com/golang/go/src go1.15 `crypto/rand`/" |
| 5 | "crypto/rsascip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/" |
| 6 | "encoding/base64scip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/" |
| 7 | "math/bigscip-go gomod github.com/golang/go/src go1.15 `math/big`/" |
| 8 | "testingscip-go gomod github.com/golang/go/src go1.15 testing/" |
| 9 | |
| 10 | "github.com/cretz/bine/torutil/ed25519scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/" |
| 11 | "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/" |
| 12 | ) |
| 13 | |
| 14 | func genRsascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/genRsa().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#, bits int) *rsascip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/PrivateKey# { |
| 15 | k, e := rsascip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/.GenerateKeyscip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/GenerateKey().(randscip-go gomod github.com/golang/go/src go1.15 `crypto/rand`/.Readerscip-go gomod github.com/golang/go/src go1.15 `crypto/rand`/Reader., bits) |
| 16 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/NoError().(t, e) |
| 17 | return k |
| 18 | } |
| 19 | |
| 20 | func genEd25519scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/genEd25519().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) ed25519scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/.KeyPairscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/KeyPair# { |
| 21 | k, e := ed25519scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/.GenerateKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/GenerateKey().(nil) |
| 22 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/NoError().(t, e) |
| 23 | return k |
| 24 | } |
| 25 | |
| 26 | func TestOnionServiceIDFromPrivateKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/TestOnionServiceIDFromPrivateKey().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) { |
| 27 | assert := func(key interface{}, shouldPanic bool) { |
| 28 | if shouldPanic { |
| 29 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.Panicsscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Panics().(t, func() { OnionServiceIDFromPrivateKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/OnionServiceIDFromPrivateKey().(key) }) |
| 30 | } else { |
| 31 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.NotPanicsscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/NotPanics().(t, func() { OnionServiceIDFromPrivateKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/OnionServiceIDFromPrivateKey().(key) }) |
| 32 | } |
| 33 | } |
| 34 | assert(nil, true) |
| 35 | assert("bad type", true) |
| 36 | assert(genRsascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/genRsa().(t, 512), true) |
| 37 | assert(genRsascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/genRsa().(t, 1024), false) |
| 38 | assert(genEd25519scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/genEd25519().(t), false) |
| 39 | } |
| 40 | |
| 41 | func TestOnionServiceIDFromPublicKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/TestOnionServiceIDFromPublicKey().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) { |
| 42 | assert := func(key interface{}, shouldPanic bool) { |
| 43 | if shouldPanic { |
| 44 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.Panicsscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Panics().(t, func() { OnionServiceIDFromPublicKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/OnionServiceIDFromPublicKey().(key) }) |
| 45 | } else { |
| 46 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.NotPanicsscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/NotPanics().(t, func() { OnionServiceIDFromPublicKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/OnionServiceIDFromPublicKey().(key) }) |
| 47 | } |
| 48 | } |
| 49 | assert(nil, true) |
| 50 | assert("bad type", true) |
| 51 | assert(genRsascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/genRsa().(t, 512).Publicscip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/PrivateKey#Public().(), true) |
| 52 | assert(genRsascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/genRsa().(t, 1024), true) |
| 53 | assert(genRsascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/genRsa().(t, 1024).Publicscip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/PrivateKey#Public().(), false) |
| 54 | assert(genEd25519scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/genEd25519().(t), true) |
| 55 | assert(genEd25519scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/genEd25519().(t).Publicscip-go gomod github.com/golang/go/src go1.15 crypto/Signer#Public().(), false) |
| 56 | } |
| 57 | |
| 58 | func TestOnionServiceIDFromV2PublicKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/TestOnionServiceIDFromV2PublicKey().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) { |
| 59 | keyNs := []string{ |
| 60 | // Good: |
| 61 | "146324450904690776677821409274235322093351159271459646966603918354799061259062657420293876128692345182038558188684966983800327732948675482476772223940488746110835191444662533167597590461666544121677987412778085089886835490778554764504249900341150942052002951429704745527158573712253866271451928082512868548761", |
| 62 | "128765593328258418045179848773717342016715415508670816023595649916344640363150769867803188216600032423256896646578968925175093584252663716464819945657362904808776223191437446288878991355616138261909405164010386485361833909203128674413041630645284466111155610996017814867550636519109125461589251061597106654453", |
| 63 | "142816677715940797613971689484332187730646681999601531244837211468050734148365138492918019219363903243436898624689103727294808675158556496441738627693945143098034304873441312947712853824963023184593797741228534339590785521072446422663170639163836372239933736851693970208563926767141632739068954958552435402293", |
| 64 | "145115352997770387235216741368218582671004692828327605746752722031765658311649572143281396789387808261614671508963042791801662334421789227429337599249357503724975792005849908733936522427330824294880823009884401313371327997012363609954851207630328042324027016587584799514594101157535904741483269310276131442141", |
| 65 | "147719637109219630754585551462675301139659936682064979504052824885582296579356301771435242063159743126441027484306731955552256555531866636211668612294755914990702770530441483651548916585382488692381916953093261634746890673551241873307767188168965986976533243218915179497387875035829308609534245761833108189053", |
| 66 | // Bad (512 bit): |
| 67 | "12406459612976799354275054531003074054562219068852891594185203203668633138039185159716483674833390801567933368800574140712590387835931746258315639847176501", |
| 68 | } |
| 69 | matchingIDs := []string{ |
| 70 | "kqxsrkmm272hqvbj", |
| 71 | "75rzoc3nxzucidqb", |
| 72 | "l2vxsdecx6yita6r", |
| 73 | "hzma3bmo7mtyr5mq", |
| 74 | "prek6tayypvteljb", |
| 75 | "", |
| 76 | } |
| 77 | for i, keyN := range keyNs { |
| 78 | pubKey := &rsascip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/PublicKey#{Escip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/PublicKey#E.: 65537, Nscip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/PublicKey#N.: new(bigscip-go gomod github.com/golang/go/src go1.15 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.15 `math/big`/Int#)} |
| 79 | pubKey.Nscip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/PublicKey#N., _ = pubKey.Nscip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/PublicKey#N..SetStringscip-go gomod github.com/golang/go/src go1.15 `math/big`/Int#SetString().(keyN, 10) |
| 80 | matchingID := matchingIDs[i] |
| 81 | if matchingID == "" { |
| 82 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.Panicsscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Panics().(t, func() { OnionServiceIDFromV2PublicKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/OnionServiceIDFromV2PublicKey().(pubKey) }) |
| 83 | } else { |
| 84 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Equal().(t, matchingID, OnionServiceIDFromV2PublicKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/OnionServiceIDFromV2PublicKey().(pubKey)) |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestOnionServiceIDFromV3PublicKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/TestOnionServiceIDFromV3PublicKey().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) { |
| 90 | base64Keys := []string{ |
| 91 | "SLne6D/uawqUj23619GbeYCd6HnzYPqyUvF8/xyz/3XNVpkgnonQI+J5NQVSGkppD1b0M87+qOtUBmVXsd7H3w", |
| 92 | "kPUs5aPoqISZVbg0q7coW+mNCODlcL4O7k2QWFOCC0gOQBiDm+g4Xz48lqucA7o2HIQ3gBdL5rlB6+q1tFdJwQ", |
| 93 | "YGzw/EwpcqfWb5UWIw652Ps4vTKu38VgX7Qo16XvOWjNWQK9YmfgARYiGQ1XYXEAKBJvoq8x+rKFbQN3FG1F6w", |
| 94 | "IJIZcWE57n5WCvHU2x7GkpBCIw0S0vWd+QyrE5RifGwPtYsbtxjyOxlb754Z0zXLZc+yQUp9hMQt5dt/YNpMag", |
| 95 | "SD7d4I6ZOjNlcqR2g4ptFJUw0tUHPQvfk92sExvnJ1uofPw9T9LUaaEs3rE/1yoGWKI4YejAzaTJXF9wrWQyuA", |
| 96 | } |
| 97 | matchingIDs := []string{ |
| 98 | "2s2wk473fmotzgh6l2ycigrwegnurlzufatjm3bglrb36zbvlerskxad", |
| 99 | "tmcpdbgklpbywqyjpr7fijvjl7qjihd7pyubosbeohefec2m2thvzoqd", |
| 100 | "nrcan5uye2fwazixubug6pzrzp6ofjez43bjcyfoxhgxyygxbhgs4zqd", |
| 101 | "g2csv4kavhunvs45vxxc5ljz775d5a4ycqo4m4nrwpk3b4gryvz2zdyd", |
| 102 | "jviaiibaz7r6wqxttj5i2bi4zjfilmsevplwwtxdfyjph2sdmq5osdid", |
| 103 | } |
| 104 | for i, base64Key := range base64Keys { |
| 105 | key, err := base64scip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/.RawStdEncodingscip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/RawStdEncoding..DecodeStringscip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/Encoding#DecodeString().(base64Key) |
| 106 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/NoError().(t, err) |
| 107 | pubKey := ed25519scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/.PrivateKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/PrivateKey#(key).PublicKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/PrivateKey#PublicKey().() |
| 108 | matchingID := matchingIDs[i] |
| 109 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Equal().(t, matchingID, OnionServiceIDFromV3PublicKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/OnionServiceIDFromV3PublicKey().(pubKey)) |
| 110 | // Check verify here too |
| 111 | derivedPubKey, err := PublicKeyFromV3OnionServiceIDscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/PublicKeyFromV3OnionServiceID().(matchingID) |
| 112 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/NoError().(t, err) |
| 113 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Equal().(t, pubKey, derivedPubKey) |
| 114 | // Let's mangle the matchingID a bit |
| 115 | tooLong := matchingID + "ddddd" |
| 116 | _, err = PublicKeyFromV3OnionServiceIDscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/PublicKeyFromV3OnionServiceID().(tooLong) |
| 117 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.EqualErrorscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/EqualError().(t, err, "Invalid id length") |
| 118 | badVersion := matchingID[:len(matchingID)-1] + "e" |
| 119 | _, err = PublicKeyFromV3OnionServiceIDscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/PublicKeyFromV3OnionServiceID().(badVersion) |
| 120 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.EqualErrorscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/EqualError().(t, err, "Invalid version") |
| 121 | badChecksum := []byte(matchingID) |
| 122 | badChecksum[len(badChecksum)-3] = 'q' |
| 123 | _, err = PublicKeyFromV3OnionServiceIDscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/PublicKeyFromV3OnionServiceID().(string(badChecksum)) |
| 124 | requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.EqualErrorscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/EqualError().(t, err, "Invalid checksum") |
| 125 | } |
| 126 | } |