From 3b6cc9016db78dfdaa9d88e4d340375c4528a8c0 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 2 Jan 2025 10:12:49 +0000 Subject: [PATCH 1/5] Update consolidated snippets --- public/icons/csharp.svg | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 public/icons/csharp.svg diff --git a/public/icons/csharp.svg b/public/icons/csharp.svg new file mode 100644 index 00000000..96cf5abc --- /dev/null +++ b/public/icons/csharp.svg @@ -0,0 +1,10 @@ + + + + + + + + + + From 2c771906f2609788b8ab3ada52b064d1c3d56096 Mon Sep 17 00:00:00 2001 From: chaitanya-jvnm Date: Thu, 2 Jan 2025 16:08:51 +0530 Subject: [PATCH 2/5] Adding c# snippets --- snippets/csharp/basics/hello-world.md | 14 +++++++++++ .../csharp/guid-utilities/generate-guid.md | 12 +++++++++ .../guid-utilities/guid-to-byte-array.md | 12 +++++++++ .../csharp/guid-utilities/validate-guid.md | 12 +++++++++ snippets/csharp/jwt-utilities/decode-jwt.md | 12 +++++++++ snippets/csharp/jwt-utilities/generate-jwt.md | 15 +++++++++++ snippets/csharp/jwt-utilities/validate-jwt.md | 25 +++++++++++++++++++ .../capitalize-first-letter.md | 15 +++++++++++ .../csharp/string-utilities/split-string.md | 12 +++++++++ 9 files changed, 129 insertions(+) create mode 100644 snippets/csharp/basics/hello-world.md create mode 100644 snippets/csharp/guid-utilities/generate-guid.md create mode 100644 snippets/csharp/guid-utilities/guid-to-byte-array.md create mode 100644 snippets/csharp/guid-utilities/validate-guid.md create mode 100644 snippets/csharp/jwt-utilities/decode-jwt.md create mode 100644 snippets/csharp/jwt-utilities/generate-jwt.md create mode 100644 snippets/csharp/jwt-utilities/validate-jwt.md create mode 100644 snippets/csharp/string-utilities/capitalize-first-letter.md create mode 100644 snippets/csharp/string-utilities/split-string.md diff --git a/snippets/csharp/basics/hello-world.md b/snippets/csharp/basics/hello-world.md new file mode 100644 index 00000000..3bca885e --- /dev/null +++ b/snippets/csharp/basics/hello-world.md @@ -0,0 +1,14 @@ +--- +title: Hello, World! +description: Prints Hello, World! to the terminal. +author: chaitanya-jvnm +tags: c#,printing,hello-world,utility +--- + +```c# +public class Program { + public static void Main(string[] args) { + System.Console.WriteLine("Hello, World!"); + } +} +``` \ No newline at end of file diff --git a/snippets/csharp/guid-utilities/generate-guid.md b/snippets/csharp/guid-utilities/generate-guid.md new file mode 100644 index 00000000..264b4f47 --- /dev/null +++ b/snippets/csharp/guid-utilities/generate-guid.md @@ -0,0 +1,12 @@ +--- +title: Hello, World! +description: Generates a new GUID +author: chaitanya-jvnm +tags: c#,guid,generate,utility +--- + +```c# +public static string GenerateGuid() { + return Guid.NewGuid().ToString(); +} +``` \ No newline at end of file diff --git a/snippets/csharp/guid-utilities/guid-to-byte-array.md b/snippets/csharp/guid-utilities/guid-to-byte-array.md new file mode 100644 index 00000000..14507538 --- /dev/null +++ b/snippets/csharp/guid-utilities/guid-to-byte-array.md @@ -0,0 +1,12 @@ +--- +title: Hello, World! +description: Converts a GUID to a byte array. +author: chaitanya-jvnm +tags: c#,guid,byte-array,utility +--- + +```c# +public static byte[] GuidToByteArray(string guid) { + return new Guid(guid).ToByteArray(); +} +``` \ No newline at end of file diff --git a/snippets/csharp/guid-utilities/validate-guid.md b/snippets/csharp/guid-utilities/validate-guid.md new file mode 100644 index 00000000..3e4e86d9 --- /dev/null +++ b/snippets/csharp/guid-utilities/validate-guid.md @@ -0,0 +1,12 @@ +--- +title: Hello, World! +description: Checks if a string is a valid GUID. +author: chaitanya-jvnm +tags: c#,guid,validate,utility +--- + +```c# +public static bool IsGuid(string str) { + return Guid.TryParse(str, out _); +} +``` \ No newline at end of file diff --git a/snippets/csharp/jwt-utilities/decode-jwt.md b/snippets/csharp/jwt-utilities/decode-jwt.md new file mode 100644 index 00000000..8adee0c1 --- /dev/null +++ b/snippets/csharp/jwt-utilities/decode-jwt.md @@ -0,0 +1,12 @@ +--- +title: Hello, World! +description: Decodes a JWT. +author: chaitanya-jvnm +tags: c#,jwt,decode,utility +--- + +```c# +public static string DecodeJwt(string token) { + return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString(); +} +``` \ No newline at end of file diff --git a/snippets/csharp/jwt-utilities/generate-jwt.md b/snippets/csharp/jwt-utilities/generate-jwt.md new file mode 100644 index 00000000..63713be8 --- /dev/null +++ b/snippets/csharp/jwt-utilities/generate-jwt.md @@ -0,0 +1,15 @@ +--- +title: Hello, World! +description: Generates a new JWT. +author: chaitanya-jvnm +tags: c#,jwt,generate,utility +--- + +```c# +public static string GenerateJwt(string secret, string issuer, string audience, int expirationMinutes) { + var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)); + var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); + var token = new JwtSecurityToken(issuer, audience, null, expires: DateTime.UtcNow.AddMinutes(expirationMinutes), signingCredentials: credentials); + return new JwtSecurityTokenHandler().WriteToken(token); +} +``` \ No newline at end of file diff --git a/snippets/csharp/jwt-utilities/validate-jwt.md b/snippets/csharp/jwt-utilities/validate-jwt.md new file mode 100644 index 00000000..94d5c8f5 --- /dev/null +++ b/snippets/csharp/jwt-utilities/validate-jwt.md @@ -0,0 +1,25 @@ +--- +title: Hello, World! +description: Validates a JWT. +author: chaitanya-jvnm +tags: c#,jwt,validate,utility +--- + +```c# +public static bool ValidateJwt(string token, string secret) { + var tokenHandler = new JwtSecurityTokenHandler(); + var validationParameters = new TokenValidationParameters { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)), + ValidateIssuer = false, + ValidateAudience = false + }; + try { + tokenHandler.ValidateToken(token, validationParameters, out _); + return true; + } + catch { + return false + } +} +``` \ No newline at end of file diff --git a/snippets/csharp/string-utilities/capitalize-first-letter.md b/snippets/csharp/string-utilities/capitalize-first-letter.md new file mode 100644 index 00000000..f129fab8 --- /dev/null +++ b/snippets/csharp/string-utilities/capitalize-first-letter.md @@ -0,0 +1,15 @@ +--- +title: Hello, World! +description: Makes the first letter of a string uppercase. +author: chaitanya-jvnm +tags: c#,string,capitalize,utility +--- + +```c# +/// +/// Capitalize the first character of the string +/// +public static string Capitalize(this string str) { + return str.Substring(0, 1).ToUpper() + str.Substring(1); +} +``` \ No newline at end of file diff --git a/snippets/csharp/string-utilities/split-string.md b/snippets/csharp/string-utilities/split-string.md new file mode 100644 index 00000000..b07593f8 --- /dev/null +++ b/snippets/csharp/string-utilities/split-string.md @@ -0,0 +1,12 @@ +--- +title: Hello, World! +description: Splits a string by a delimiter. +author: chaitanya-jvnm +tags: c#,string,split,utility +--- + +```c# +public static string[] SplitString(string str, string delimiter) { + return str.Split(delimiter); +} +``` \ No newline at end of file From 94962ab8ddeb856ae859278a9ee4f8aec05f159f Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 2 Jan 2025 10:39:30 +0000 Subject: [PATCH 3/5] Update consolidated snippets --- public/consolidated/csharp.json | 132 ++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/public/consolidated/csharp.json b/public/consolidated/csharp.json index 16ddfbea..4e6031a8 100644 --- a/public/consolidated/csharp.json +++ b/public/consolidated/csharp.json @@ -1,4 +1,110 @@ [ + { + "categoryName": "Basics", + "snippets": [ + { + "title": "Hello, World!", + "description": "Prints Hello, World! to the terminal.", + "author": "chaitanya-jvnm", + "tags": [ + "c#", + "printing", + "hello-world", + "utility" + ], + "contributors": [], + "code": "public class Program {\n public static void Main(string[] args) {\n System.Console.WriteLine(\"Hello, World!\");\n }\n}\n" + } + ] + }, + { + "categoryName": "Guid Utilities", + "snippets": [ + { + "title": "Hello, World!", + "description": "Generates a new GUID", + "author": "chaitanya-jvnm", + "tags": [ + "c#", + "guid", + "generate", + "utility" + ], + "contributors": [], + "code": "public static string GenerateGuid() {\n return Guid.NewGuid().ToString();\n}\n" + }, + { + "title": "Hello, World!", + "description": "Converts a GUID to a byte array.", + "author": "chaitanya-jvnm", + "tags": [ + "c#", + "guid", + "byte-array", + "utility" + ], + "contributors": [], + "code": "public static byte[] GuidToByteArray(string guid) {\n return new Guid(guid).ToByteArray();\n}\n" + }, + { + "title": "Hello, World!", + "description": "Checks if a string is a valid GUID.", + "author": "chaitanya-jvnm", + "tags": [ + "c#", + "guid", + "validate", + "utility" + ], + "contributors": [], + "code": "public static bool IsGuid(string str) {\n return Guid.TryParse(str, out _);\n}\n" + } + ] + }, + { + "categoryName": "Jwt Utilities", + "snippets": [ + { + "title": "Hello, World!", + "description": "Decodes a JWT.", + "author": "chaitanya-jvnm", + "tags": [ + "c#", + "jwt", + "decode", + "utility" + ], + "contributors": [], + "code": "public static string DecodeJwt(string token) {\n return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();\n}\n" + }, + { + "title": "Hello, World!", + "description": "Generates a new JWT.", + "author": "chaitanya-jvnm", + "tags": [ + "c#", + "jwt", + "generate", + "utility" + ], + "contributors": [], + "code": "public static string GenerateJwt(string secret, string issuer, string audience, int expirationMinutes) {\n var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret));\n var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);\n var token = new JwtSecurityToken(issuer, audience, null, expires: DateTime.UtcNow.AddMinutes(expirationMinutes), signingCredentials: credentials);\n return new JwtSecurityTokenHandler().WriteToken(token);\n}\n" + }, + { + "title": "Hello, World!", + "description": "Validates a JWT.", + "author": "chaitanya-jvnm", + "tags": [ + "c#", + "jwt", + "validate", + "utility" + ], + "contributors": [], + "code": "public static bool ValidateJwt(string token, string secret) {\n var tokenHandler = new JwtSecurityTokenHandler();\n var validationParameters = new TokenValidationParameters {\n ValidateIssuerSigningKey = true,\n IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),\n ValidateIssuer = false,\n ValidateAudience = false\n };\n try {\n tokenHandler.ValidateToken(token, validationParameters, out _);\n return true;\n }\n catch {\n return false\n }\n}\n" + } + ] + }, { "categoryName": "List Utilities", "snippets": [ @@ -20,6 +126,32 @@ { "categoryName": "String Utilities", "snippets": [ + { + "title": "Hello, World!", + "description": "Makes the first letter of a string uppercase.", + "author": "chaitanya-jvnm", + "tags": [ + "c#", + "string", + "capitalize", + "utility" + ], + "contributors": [], + "code": "/// \n/// Capitalize the first character of the string\n/// \npublic static string Capitalize(this string str) {\n return str.Substring(0, 1).ToUpper() + str.Substring(1);\n}\n" + }, + { + "title": "Hello, World!", + "description": "Splits a string by a delimiter.", + "author": "chaitanya-jvnm", + "tags": [ + "c#", + "string", + "split", + "utility" + ], + "contributors": [], + "code": "public static string[] SplitString(string str, string delimiter) {\n return str.Split(delimiter);\n}\n" + }, { "title": "Truncate a String", "description": "Cut off a string once it reaches a determined amount of characters and add '...' to the end of the string", From 59eb5169a25d264467984af738af1a6bf9089edb Mon Sep 17 00:00:00 2001 From: chaitanya-jvnm Date: Thu, 2 Jan 2025 16:39:10 +0530 Subject: [PATCH 4/5] Added Examples and Removed straightforward snippets --- snippets/csharp/guid-utilities/guid-to-byte-array.md | 12 ------------ snippets/csharp/jwt-utilities/decode-jwt.md | 10 ++++++++++ snippets/csharp/jwt-utilities/validate-jwt.md | 10 ++++++++++ .../string-utilities/capitalize-first-letter.md | 5 +++++ snippets/csharp/string-utilities/split-string.md | 12 ------------ 5 files changed, 25 insertions(+), 24 deletions(-) delete mode 100644 snippets/csharp/guid-utilities/guid-to-byte-array.md delete mode 100644 snippets/csharp/string-utilities/split-string.md diff --git a/snippets/csharp/guid-utilities/guid-to-byte-array.md b/snippets/csharp/guid-utilities/guid-to-byte-array.md deleted file mode 100644 index 14507538..00000000 --- a/snippets/csharp/guid-utilities/guid-to-byte-array.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -title: Hello, World! -description: Converts a GUID to a byte array. -author: chaitanya-jvnm -tags: c#,guid,byte-array,utility ---- - -```c# -public static byte[] GuidToByteArray(string guid) { - return new Guid(guid).ToByteArray(); -} -``` \ No newline at end of file diff --git a/snippets/csharp/jwt-utilities/decode-jwt.md b/snippets/csharp/jwt-utilities/decode-jwt.md index 8adee0c1..c4c77999 100644 --- a/snippets/csharp/jwt-utilities/decode-jwt.md +++ b/snippets/csharp/jwt-utilities/decode-jwt.md @@ -6,7 +6,17 @@ tags: c#,jwt,decode,utility --- ```c# +/// +/// Decodes the JWT +/// public static string DecodeJwt(string token) { return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString(); } + +//Example +string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; + +string decodedJwt = DecodeJwt(token); + +Console.WriteLine(decodedJwt); //Prints {"alg":"HS256","typ":"JWT"}.{"sub":"1234567890","name":"John Doe","iat":1516239022} ``` \ No newline at end of file diff --git a/snippets/csharp/jwt-utilities/validate-jwt.md b/snippets/csharp/jwt-utilities/validate-jwt.md index 94d5c8f5..ea48a8f5 100644 --- a/snippets/csharp/jwt-utilities/validate-jwt.md +++ b/snippets/csharp/jwt-utilities/validate-jwt.md @@ -22,4 +22,14 @@ public static bool ValidateJwt(string token, string secret) { return false } } + +//Example +string JWT = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; + +string correctSecret = "your-256-bit-secret"; +string wrongSecret = "this-is-not-the-right-secret"; + +Console.WriteLine(ValidateJwt(JWT, correctSecret)) // returns True +Console.WriteLine(ValidateJwt(JWT, wrongSecret)) // returns False + ``` \ No newline at end of file diff --git a/snippets/csharp/string-utilities/capitalize-first-letter.md b/snippets/csharp/string-utilities/capitalize-first-letter.md index f129fab8..b69afe40 100644 --- a/snippets/csharp/string-utilities/capitalize-first-letter.md +++ b/snippets/csharp/string-utilities/capitalize-first-letter.md @@ -12,4 +12,9 @@ tags: c#,string,capitalize,utility public static string Capitalize(this string str) { return str.Substring(0, 1).ToUpper() + str.Substring(1); } + +//Example +string example = "hello"; +string captializedExample = example.Capitalize(); +Console.WriteLine(captializedExample); // prints "Hello" ``` \ No newline at end of file diff --git a/snippets/csharp/string-utilities/split-string.md b/snippets/csharp/string-utilities/split-string.md deleted file mode 100644 index b07593f8..00000000 --- a/snippets/csharp/string-utilities/split-string.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -title: Hello, World! -description: Splits a string by a delimiter. -author: chaitanya-jvnm -tags: c#,string,split,utility ---- - -```c# -public static string[] SplitString(string str, string delimiter) { - return str.Split(delimiter); -} -``` \ No newline at end of file From 55bd1e591922c54b2c2a5df26f86b8bf7c884613 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 2 Jan 2025 11:10:12 +0000 Subject: [PATCH 5/5] Update consolidated snippets --- public/consolidated/csharp.json | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/public/consolidated/csharp.json b/public/consolidated/csharp.json index 4e6031a8..84cac2d5 100644 --- a/public/consolidated/csharp.json +++ b/public/consolidated/csharp.json @@ -33,19 +33,6 @@ "contributors": [], "code": "public static string GenerateGuid() {\n return Guid.NewGuid().ToString();\n}\n" }, - { - "title": "Hello, World!", - "description": "Converts a GUID to a byte array.", - "author": "chaitanya-jvnm", - "tags": [ - "c#", - "guid", - "byte-array", - "utility" - ], - "contributors": [], - "code": "public static byte[] GuidToByteArray(string guid) {\n return new Guid(guid).ToByteArray();\n}\n" - }, { "title": "Hello, World!", "description": "Checks if a string is a valid GUID.", @@ -75,7 +62,7 @@ "utility" ], "contributors": [], - "code": "public static string DecodeJwt(string token) {\n return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();\n}\n" + "code": "/// \n/// Decodes the JWT\n/// \npublic static string DecodeJwt(string token) {\n return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();\n}\n\n//Example\nstring token = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\n\nstring decodedJwt = DecodeJwt(token);\n\nConsole.WriteLine(decodedJwt); //Prints {\"alg\":\"HS256\",\"typ\":\"JWT\"}.{\"sub\":\"1234567890\",\"name\":\"John Doe\",\"iat\":1516239022}\n" }, { "title": "Hello, World!", @@ -101,7 +88,7 @@ "utility" ], "contributors": [], - "code": "public static bool ValidateJwt(string token, string secret) {\n var tokenHandler = new JwtSecurityTokenHandler();\n var validationParameters = new TokenValidationParameters {\n ValidateIssuerSigningKey = true,\n IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),\n ValidateIssuer = false,\n ValidateAudience = false\n };\n try {\n tokenHandler.ValidateToken(token, validationParameters, out _);\n return true;\n }\n catch {\n return false\n }\n}\n" + "code": "public static bool ValidateJwt(string token, string secret) {\n var tokenHandler = new JwtSecurityTokenHandler();\n var validationParameters = new TokenValidationParameters {\n ValidateIssuerSigningKey = true,\n IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),\n ValidateIssuer = false,\n ValidateAudience = false\n };\n try {\n tokenHandler.ValidateToken(token, validationParameters, out _);\n return true;\n }\n catch {\n return false\n }\n}\n\n//Example\nstring JWT = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\n\nstring correctSecret = \"your-256-bit-secret\";\nstring wrongSecret = \"this-is-not-the-right-secret\";\n\nConsole.WriteLine(ValidateJwt(JWT, correctSecret)) // returns True\nConsole.WriteLine(ValidateJwt(JWT, wrongSecret)) // returns False\n\n" } ] }, @@ -137,20 +124,7 @@ "utility" ], "contributors": [], - "code": "/// \n/// Capitalize the first character of the string\n/// \npublic static string Capitalize(this string str) {\n return str.Substring(0, 1).ToUpper() + str.Substring(1);\n}\n" - }, - { - "title": "Hello, World!", - "description": "Splits a string by a delimiter.", - "author": "chaitanya-jvnm", - "tags": [ - "c#", - "string", - "split", - "utility" - ], - "contributors": [], - "code": "public static string[] SplitString(string str, string delimiter) {\n return str.Split(delimiter);\n}\n" + "code": "/// \n/// Capitalize the first character of the string\n/// \npublic static string Capitalize(this string str) {\n return str.Substring(0, 1).ToUpper() + str.Substring(1);\n}\n\n//Example\nstring example = \"hello\";\nstring captializedExample = example.Capitalize();\nConsole.WriteLine(captializedExample); // prints \"Hello\"\n" }, { "title": "Truncate a String",