Utilizing AI models like GPT for translating between programming languages offers several advantages:
While these benefits are promising, it's worth noting that automated translation between programming languages is a complex task and may not always be perfect, especially for complex programs. Human oversight will still be essential to ensure the translated code behaves as expected.
Convert the following code from Typescript to C#
import * as bcrypt from 'bcrypt';
class UserAuth {
static validateEmail(email: string): boolean {
let emailRegex = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,}$/;
return emailRegex.test(email);
}
static validatePassword(password: string): boolean {
let passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,}$/;
return passwordRegex.test(password);
}
static splitEmail(email: string): [string, string, string] {
const [username, domain] = email.split('@');
const domainParts = domain.split('.');
const topLevelDomain = domainParts[domainParts.length - 1];
return [username, domain, topLevelDomain];
}
static async hashPassword(password: string): Promise<string> {
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
return hashedPassword;
}
static async validateHash(password: string, hash: string): Promise<boolean> {
return await bcrypt.compare(password, hash);
}
}
In this case, the task was to convert TypeScript code into equivalent C# code, focusing on the key features used in the user authentication class. This involved translating not only the overall syntax but also understanding how key features like regular expressions, string splitting, async methods, and array initialisation work in both languages.
Here's how our TypeScript code would translate into C#:
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BCrypt.Net;
public class UserAuth {
public static bool ValidateEmail(string email) {
string emailRegex = @"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,}$";
return Regex.IsMatch(email, emailRegex);
}
public static bool ValidatePassword(string password) {
string passwordRegex = @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,}$";
return Regex.IsMatch(password, passwordRegex);
}
public static (string, string, string) SplitEmail(string email)
{
int atIndex = email.IndexOf('@');
int lastDotIndex = email.LastIndexOf('.');
if (atIndex == -1 || lastDotIndex == -1 || atIndex > lastDotIndex)
{
throw new ArgumentException("Invalid email format");
}
string username = email.Substring(0, atIndex);
string domain = email.Substring(atIndex + 1);
string topLevelDomain = email.Substring(lastDotIndex + 1);
return (username, domain, topLevelDomain);
}
public static async Task<string> HashPassword(string password) {
string salt = BCrypt.Net.BCrypt.GenerateSalt(10);
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(password, salt);
return hashedPassword;
}
public static async Task<bool> ValidateHash(string password, string hash) {
return BCrypt.Net.BCrypt.Verify(password, hash);
}
}
The C# equivalent code maintains the structure of the original TypeScript code, using static methods within a class. However, there are a few key differences:
Regex
class from the System.Text.RegularExpressions
namespace to handle regular expressions.string.Split()
function in C# is equivalent to split()
in TypeScript.async
keyword and Task
to define asynchronous methods.
But the BCrypt methods used here are synchronous in C#, unlike their counterparts in JavaScript.new
keyword to initialize it.The ability to translate code between languages is a valuable skill in software development. It helps you understand the strengths and limitations of different languages and broadens your potential toolset.
While C# and TypeScript have different features and syntax, they both can effectively handle tasks such as user authentication. This example has shown how similar functionality can be implemented in these two languages, providing a stepping stone for further exploration.
If you found this blog post helpful, feel free to check out our other blog posts on using AI in software development at the Logobean Blog!
Add your business name to instantly generate an endless selection of logos and brands.
Select your logo styles to refine the generated logos, click any logo to view it in the live previews & logo style guide and favorite the logos that you love.
Edit any logo to perfection using our intuitive logo and rich text editors.
Once you've found the perfect logo, download and use your logo package instantly!