
Practice 50 Go Lang Security in Go Coding Questions
Q1. Write a Go program to encrypt and decrypt a string using AES encryption.
Input:
"hello, world"
Expected Output:
Encrypted Text: [encrypted string]
Decrypted Text: hello, world
Q2. Write a Go program to hash a password using bcrypt.
Input:
Password: "mysecretpassword"
Expected Output:
Hashed Password: $2a$10$...
Q3. Write a Go program that generates a JWT token.
Input:
Username: "user1", Role: "admin"
Expected Output:
JWT Token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Q4. Write a Go program to securely compare two passwords.
Input:
Password1: "mypassword", Password2: "mypassword"
Expected Output:
Passwords match: true
Q5. Write a Go program to generate a random salt for password hashing.
Input:
Generate salt for hashing.
Expected Output:
Salt: "random-salt-value"
Q6. Write a Go program to perform HMAC-SHA256 hashing.
Input:
Message: "secure message", Key: "secretkey"
Expected Output:
HMAC-SHA256 Hash: [hashed value]
Q7. Write a Go program to generate a secure random number.
Input:
Generate a random number.
Expected Output:
Random Number: 25401
Q8. Write a Go program that demonstrates SSL/TLS encryption for a web server.
Input:
Set up an HTTPS server with a valid SSL certificate.
Expected Output:
Server is running on https://localhost:443
Q9. Write a Go program to create and verify digital signatures.
Input:
Message: "This is a signed message", Private Key: "private-key"
Expected Output:
Signature: [signature]
Signature verified: true
Q10. Write a Go program to prevent SQL Injection by using parameterized queries.
Input:
Query: "SELECT FROM users WHERE username = ?"
Parameters: "admin"
Expected Output:
Query executed safely, no SQL injection.
Q11. Write a Go program to sanitize user input to prevent XSS attacks.
Input:
User Input: "<script>alert('hack');</script>"
Expected Output:
Sanitized Input: <script>alert('hack');</script>
Q12. Write a Go program to securely store a password in an environment variable.
Input:
Set environment variable: "PASSWORD=mysecret"
Expected Output:
Password stored securely in environment variable.
Q13. Write a Go program to encrypt a file using AES encryption.
Input:
File: "data.txt", Key: "secretkey"
Expected Output:
File encrypted successfully: data.encrypted
Q14. Write a Go program to securely delete a file.
Input:
File: "data.txt"
Expected Output:
File "data.txt" securely deleted.
Q15. Write a Go program that demonstrates how to securely handle sensitive data in memory.
Input:
Store password in a secure, zeroed-out variable after use.
Expected Output:
Sensitive data handled securely, memory cleared.
Q16. Write a Go program that checks the strength of a password.
Input:
Password: "P@ssw0rd123"
Expected Output:
Password strength: Strong
Q17. Write a Go program to implement basic access control for a web application.
Input:
User Role: "admin", Action: "delete"
Expected Output:
Access granted: Admin can delete.
Q18. Write a Go program to generate and verify an OTP (One-Time Password).
Input:
Generate OTP with key: "secret"
Expected Output:
OTP: 123456
OTP verified: true
Q19. Write a Go program to perform input validation and reject unsafe inputs.
Input:
User Input: "DROP TABLE users;"
Expected Output:
Invalid input detected: SQL injection attempt.
Q20. Write a Go program that uses AES encryption for file integrity checking.
Input:
File: "file.txt", Key: "secretkey"
Expected Output:
File encrypted, integrity checked: [encrypted checksum]
Q21. Write a Go program to implement rate-limiting for a web API to prevent abuse.
Input:
User makes 5 requests per minute.
Expected Output:
Rate limit exceeded: Please try again later.
Q22. Write a Go program to use HTTPS with self-signed certificates.
Input:
Configure a self-signed certificate for HTTPS server.
Expected Output:
Server running on https://localhost:443 with self-signed certificate.
Q23. Write a Go program that uses TLS to establish a secure client-server communication.
Input:
TLS handshake between client and server.
Expected Output:
TLS connection established securely.
Q24. Write a Go program to hash a message with SHA-256.
Input:
Message: "Hello GoLang"
Expected Output:
SHA-256 Hash: [hashed value]
Q25. Write a Go program to generate a secure password with a random generator.
Input:
Password Length: 12
Expected Output:
Generated Password: lT!4xv9uCzY!
Q26. Write a Go program to secure an HTTP connection using HTTP headers like Content Security Policy (CSP).
Input:
Set CSP header for HTTP response.
Expected Output:
CSP Header: default-src 'self'
Q27. Write a Go program that uses the crypto/rand package for generating secure random values.
Input:
Generate secure random string of 16 characters.
Expected Output:
Secure Random Value: A9Z0Dg2HqW3j2k8L
Q28. Write a Go program to encrypt and decrypt a string using RSA keys.
Input:
Message: "Confidential data", Private Key: "private-key", Public Key: "public-key"
Expected Output:
Encrypted Message: [encrypted data]
Decrypted Message: Confidential data
Q29. Write a Go program to prevent command injection by sanitizing input.
Input:
User Input: "rm -rf /"
Expected Output:
Invalid command detected: Command injection attempt.
Q30. Write a Go program to implement CSRF protection in a web application.
Input:
Request contains CSRF token and user validation.
Expected Output:
CSRF token validated: Request processed.
Q31. Write a Go program that generates and validates a secure session ID.
Input:
Session ID: "session123"
Expected Output:
Session validated successfully.
Q32. Write a Go program to handle HTTP basic authentication.
Input:
Username: "admin", Password: "password123"
Expected Output:
Authentication successful.
Q33. Write a Go program to prevent directory traversal attacks.
Input:
User Input: "../../etc/passwd"
Expected Output:
Invalid path detected: Directory traversal attempt.
Q34. Write a Go program that uses crypto/sha512 to hash a string.
Input:
String: "GoLang Security"
Expected Output:
SHA-512 Hash: [hashed value]
Q35. Write a Go program to perform input validation for email addresses.
Input:
Email: "user@example.com"
Expected Output:
Valid email address: user@example.com
Q36. Write a Go program to secure the communication channel with SSH.
Input:
Set up SSH key authentication for secure login.
Expected Output:
SSH connection established securely.
Q37. Write a Go program to prevent XSS by encoding HTML special characters.
Input:
User Input: "<img src=x onerror=alert('XSS')>"
Expected Output:
Encoded Input: <img src=x onerror=alert('XSS')>
Q38. Write a Go program to generate a unique session ID for each user.
Input:
User: "JohnDoe"
Expected Output:
Generated Session ID: 12c34fbc72ad2f38
Q39. Write a Go program to implement multi-factor authentication (MFA).
Input:
User enters password, then OTP is sent for validation.
Expected Output:
MFA Successful: User authenticated.
Q40. Write a Go program that uses SSL/TLS to protect HTTP communication.
Input:
Set up SSL/TLS for HTTP server.
Expected Output:
Secure connection established over HTTPS.
Q41. Write a Go program to handle the encryption and decryption of database credentials.
Input:
Database Username: "admin", Password: "dbpass"
Expected Output:
Encrypted credentials stored securely.
Q42. Write a Go program that implements a CAPTCHA for form submission.
Input:
User enters CAPTCHA code "AB12CD"
Expected Output:
CAPTCHA validated: Submission successful.
Q43. Write a Go program to securely generate an API key.
Input:
Generate API key for "user1".
Expected Output:
Generated API key: abcd1234xyz
Q44. Write a Go program to store sensitive data in a secure vault (e.g., Vault by HashiCorp).
Input:
Store API key in secure vault.
Expected Output:
Sensitive data stored securely in vault.
Q45. Write a Go program to demonstrate key-based encryption and decryption using RSA.
Input:
Public Key: [public-key], Private Key: [private-key], Message: "Secure Message"
Expected Output:
RSA Encrypted: [encrypted data]
RSA Decrypted: Secure Message
Q46. Write a Go program that encrypts a string with the RSA public key and decrypts it using the private key.
Input:
Message: "GoLang Encryption"
Expected Output:
RSA Encryption: [encrypted data]
RSA Decryption: GoLang Encryption
Q47. Write a Go program to perform authentication using OAuth2.
Input:
User: "admin", OAuth Token: "token123"
Expected Output:
Authentication successful using OAuth2.
Q48. Write a Go program to securely communicate with a database over SSL.
Input:
Connect to database with SSL enabled.
Expected Output:
Database connection secured using SSL.
Q49. Write a Go program to generate a secure encryption key with crypto/rand.
Input:
Generate a secure key for AES encryption.
Expected Output:
Generated Key: [random secure key]
Q50. Write a Go program to handle sensitive data storage using a secure vault.
Input:
Store sensitive information such as passwords in a secure vault.
Expected Output:
Sensitive data securely stored in the vault.