| 1 | package pkcs7scip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.19 bytes/" |
| 5 | "crypto/x509scip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/" |
| 6 | "testingscip-go gomod github.com/golang/go/src go1.19 testing/" |
| 7 | ) |
| 8 | |
| 9 | func TestEncryptscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/TestEncrypt().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 10 | modes := []int{ |
| 11 | EncryptionAlgorithmDESCBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmDESCBC., |
| 12 | EncryptionAlgorithmAES128CBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES128CBC., |
| 13 | EncryptionAlgorithmAES256CBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES256CBC., |
| 14 | EncryptionAlgorithmAES128GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES128GCM., |
| 15 | EncryptionAlgorithmAES256GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES256GCM., |
| 16 | } |
| 17 | sigalgs := []x509scip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/.SignatureAlgorithmscip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/SignatureAlgorithm#{ |
| 18 | x509scip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/.SHA1WithRSAscip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/SHA1WithRSA., |
| 19 | x509scip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/.SHA256WithRSAscip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/SHA256WithRSA., |
| 20 | x509scip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/.SHA512WithRSAscip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/SHA512WithRSA., |
| 21 | } |
| 22 | for _, mode := range modes { |
| 23 | for _, sigalg := range sigalgs { |
| 24 | ContentEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ContentEncryptionAlgorithm. = mode |
| 25 | |
| 26 | plaintext := []byte("Hello Secret World!") |
| 27 | cert, err := createTestCertificatescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/createTestCertificate().(sigalg) |
| 28 | if err != nil { |
| 29 | t.Fatalscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatal().(err) |
| 30 | } |
| 31 | encrypted, err := Encryptscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/Encrypt().(plaintext, []*x509scip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/Certificate#{cert.Certificatescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/certKeyPair#Certificate.}) |
| 32 | if err != nil { |
| 33 | t.Fatalscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatal().(err) |
| 34 | } |
| 35 | p7, err := Parsescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/Parse().(encrypted) |
| 36 | if err != nil { |
| 37 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("cannot Parse encrypted result: %s", err) |
| 38 | } |
| 39 | result, err := p7.Decryptscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/PKCS7#Decrypt().(cert.Certificatescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/certKeyPair#Certificate., *cert.PrivateKeyscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/certKeyPair#PrivateKey.) |
| 40 | if err != nil { |
| 41 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("cannot Decrypt encrypted result: %s", err) |
| 42 | } |
| 43 | if !bytesscip-go gomod github.com/golang/go/src go1.19 bytes/.Equalscip-go gomod github.com/golang/go/src go1.19 bytes/Equal().(plaintext, result) { |
| 44 | t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("encrypted data does not match plaintext:\n\tExpected: %s\n\tActual: %s", plaintext, result) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestEncryptUsingPSKscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/TestEncryptUsingPSK().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 51 | modes := []int{ |
| 52 | EncryptionAlgorithmDESCBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmDESCBC., |
| 53 | EncryptionAlgorithmAES128GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES128GCM., |
| 54 | } |
| 55 | |
| 56 | for _, mode := range modes { |
| 57 | ContentEncryptionAlgorithmscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/ContentEncryptionAlgorithm. = mode |
| 58 | plaintext := []byte("Hello Secret World!") |
| 59 | var key []byte |
| 60 | |
| 61 | switch mode { |
| 62 | case EncryptionAlgorithmDESCBCscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmDESCBC.: |
| 63 | key = []byte("64BitKey") |
| 64 | case EncryptionAlgorithmAES128GCMscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptionAlgorithmAES128GCM.: |
| 65 | key = []byte("128BitKey4AESGCM") |
| 66 | } |
| 67 | ciphertext, err := EncryptUsingPSKscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/EncryptUsingPSK().(plaintext, key) |
| 68 | if err != nil { |
| 69 | t.Fatalscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatal().(err) |
| 70 | } |
| 71 | |
| 72 | p7, _ := Parsescip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/Parse().(ciphertext) |
| 73 | result, err := p7.DecryptUsingPSKscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/PKCS7#DecryptUsingPSK().(key) |
| 74 | if err != nil { |
| 75 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("cannot Decrypt encrypted result: %s", err) |
| 76 | } |
| 77 | if !bytesscip-go gomod github.com/golang/go/src go1.19 bytes/.Equalscip-go gomod github.com/golang/go/src go1.19 bytes/Equal().(plaintext, result) { |
| 78 | t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("encrypted data does not match plaintext:\n\tExpected: %s\n\tActual: %s", plaintext, result) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestPadscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/TestPad().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 84 | tests := []struct { |
| 85 | Original []byte |
| 86 | Expected []byte |
| 87 | BlockSize int |
| 88 | }{ |
| 89 | {[]byte{0x1, 0x2, 0x3, 0x10}, []byte{0x1, 0x2, 0x3, 0x10, 0x4, 0x4, 0x4, 0x4}, 8}, |
| 90 | {[]byte{0x1, 0x2, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0}, []byte{0x1, 0x2, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8}, 8}, |
| 91 | } |
| 92 | for _, test := range tests { |
| 93 | padded, err := padscip-go gomod github.com/digitorus/pkcs7 . `github.com/digitorus/pkcs7`/pad().(test.Original, test.BlockSize) |
| 94 | if err != nil { |
| 95 | t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("pad encountered error: %s", err) |
| 96 | continue |
| 97 | } |
| 98 | if !bytesscip-go gomod github.com/golang/go/src go1.19 bytes/.Equalscip-go gomod github.com/golang/go/src go1.19 bytes/Equal().(test.Expected, padded) { |
| 99 | t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("pad results mismatch:\n\tExpected: %X\n\tActual: %X", test.Expected, padded) |
| 100 | } |
| 101 | } |
| 102 | } |