Skip to content

Commit afad8ff

Browse files
authored
Fix: A few structural changes to the repository and added missing tests and fixed some failing tests (TheAlgorithms#325)
* fix: RSA algorithm * fix: rename xor cipher directory for better readability * fix: sorting algorithms failing tests * fix: linting issues * fix: run gofmt * fix: linting issues * Update golang_lint_and_test.yml * Add Taj to CODEOWNERS
1 parent 4037fd7 commit afad8ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+770
-674
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77

88
# Order is important. The last matching pattern has the most precedence.
99

10-
* @siriak @raklaptudirm
10+
* @siriak @raklaptudirm @tjgurwara99
1111
/.* @cclauss

.github/workflows/golang_lint_and_test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ jobs:
1515
# Required: the version of golangci-lint is required and must be
1616
# specified without patch version: we always use the latest patch version.
1717
# https://github.com/golangci/golangci-lint/releases
18-
version: v1.39
18+
version: latest
19+
args: -E gofmt
1920
- run: go test ./...

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
1919
* [Diffie Hellman Key Exchange](./ciphers/diffie_hellman_key_exchange/)
2020
* [Polybius](./ciphers/polybius/)
2121
* [Rot13](./ciphers/rot13/)
22-
* [Rsa Cipher](./ciphers/rsa_cipher/)
23-
* [Xor Cipher](./ciphers/xor_cipher/)
22+
* [Rsa](./ciphers/rsa/)
23+
* [Xor](./ciphers/xor/)
2424

2525
## Conversions
2626
* [Roman To Integer](./conversions/roman_to_integer/)
@@ -64,7 +64,7 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
6464
## Math
6565
* [Gcd](./math/gcd/)
6666
* [Lcm](./math/lcm/)
67-
* [Modular Arithmetic](./math/modular_arithmetic/)
67+
* [Modular](./math/modular/)
6868
* [Permutation](./math/permutation/)
6969
* [Power](./math/power/)
7070
* [Prime](./math/prime/)

ciphers/rsa/rsa.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// rsa.go
2+
// description: Simple RSA algorithm implementation
3+
// details:
4+
// A simple RSA Encryption and Decryption algorithm.
5+
// It uses prime numbers that fit in int64 datatypes and
6+
// thus both the Encrypt and Decrypt are not a production
7+
// ready implementation. The OpenSSL implementation of RSA
8+
// also adds a padding which is not present in this algorithm.
9+
// author(s) [Taj](https://github.com/tjgurwara99)
10+
// see rsa_test.go
11+
12+
// Package rsa shows a simple implementation of RSA algorithm
13+
package rsa
14+
15+
import (
16+
"errors"
17+
18+
modular "github.com/TheAlgorithms/Go/math/modular"
19+
)
20+
21+
// ErrorFailedToEncrypt Raised when Encrypt function fails to encrypt the message
22+
var ErrorFailedToEncrypt = errors.New("failed to Encrypt")
23+
24+
// ErrorFailedToDecrypt Raised when Decrypt function fails to decrypt the encrypted message
25+
var ErrorFailedToDecrypt = errors.New("failed to Decrypt")
26+
27+
// Encrypt encrypts based on the RSA algorithm - uses modular exponentitation in math directory
28+
func Encrypt(message []rune, publicExponent, modulus int64) ([]rune, error) {
29+
var encrypted []rune
30+
31+
for _, letter := range message {
32+
encryptedLetter, err := modular.Exponentiation(int64(letter), publicExponent, modulus)
33+
if err != nil {
34+
return nil, ErrorFailedToEncrypt
35+
}
36+
encrypted = append(encrypted, rune(encryptedLetter))
37+
}
38+
39+
return encrypted, nil
40+
}
41+
42+
// Decrypt decrypts encrypted rune slice based on the RSA algorithm
43+
func Decrypt(encrypted []rune, privateExponent, modulus int64) (string, error) {
44+
var decrypted []rune
45+
46+
for _, letter := range encrypted {
47+
decryptedLetter, err := modular.Exponentiation(int64(letter), privateExponent, modulus)
48+
if err != nil {
49+
return "", ErrorFailedToDecrypt
50+
}
51+
decrypted = append(decrypted, rune(decryptedLetter))
52+
}
53+
return string(decrypted), nil
54+
}

ciphers/rsa/rsa_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// rsa_test.go
2+
// description: Test for RSA Encrypt and Decrypt algorithms
3+
// author(s) [Taj](https://github.com/tjgurwara99)
4+
// see rsa.go
5+
6+
package rsa
7+
8+
import (
9+
"testing"
10+
11+
"github.com/TheAlgorithms/Go/math/gcd"
12+
"github.com/TheAlgorithms/Go/math/lcm"
13+
"github.com/TheAlgorithms/Go/math/modular"
14+
)
15+
16+
var rsaTestData = []struct {
17+
description string
18+
input string
19+
}{
20+
{
21+
"Encrypt letter 'a'",
22+
"a",
23+
},
24+
{
25+
"Encrypt 'hello world'",
26+
"hello world",
27+
},
28+
{
29+
"Encrypt full sentence",
30+
"the quick brown fox jumps over the lazy dog.",
31+
},
32+
{
33+
"Encrypt full sentence from rsacipher.go main function",
34+
"I think RSA is really great",
35+
},
36+
}
37+
38+
func TestEncryptDecrypt(t *testing.T) {
39+
// Both prime numbers
40+
p := int64(61)
41+
q := int64(53)
42+
43+
n := p * q
44+
45+
delta := lcm.Lcm(p-1, q-1)
46+
47+
e := int64(17) // Coprime with delta
48+
49+
if gcd.Recursive(e, delta) != 1 {
50+
t.Fatal("Algorithm failed in preamble stage:\n\tPrime numbers are chosed statically and it shouldn't fail at this stage")
51+
}
52+
53+
d, err := modular.Inverse(e, delta)
54+
55+
if err != nil {
56+
t.Fatalf("Algorithm failed in preamble stage:\n\tProblem with a modular directory dependency: %v", err)
57+
}
58+
59+
for _, test := range rsaTestData {
60+
t.Run(test.description, func(t *testing.T) {
61+
62+
message := []rune(test.input)
63+
encrypted, err := Encrypt(message, e, n)
64+
if err != nil {
65+
t.Fatalf("Failed to Encrypt test string:\n\tDescription: %v\n\tErrMessage: %v", test.description, err)
66+
}
67+
68+
decrypted, err := Decrypt(encrypted, d, n)
69+
if err != nil {
70+
t.Fatalf("Failed to Decrypt test message:\n\tDescription: %v\n\tErrMessage: %v", test.description, err)
71+
}
72+
73+
if actual := test.input; actual != decrypted {
74+
t.Logf("FAIL: %s", test.description)
75+
t.Fatalf("Expecting %v, actual %v", decrypted, actual)
76+
}
77+
})
78+
}
79+
}

ciphers/rsa_cipher/rsacipher.go

Lines changed: 0 additions & 195 deletions
This file was deleted.

0 commit comments

Comments
 (0)