From adea0017f79ea38550a94d681b31d5e480a061d4 Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Tue, 25 Aug 2020 12:09:15 +0100 Subject: [PATCH 1/9] ft: update nuget info --- shortid/shortid.csproj | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/shortid/shortid.csproj b/shortid/shortid.csproj index 31ff0b2..9bac8ad 100644 --- a/shortid/shortid.csproj +++ b/shortid/shortid.csproj @@ -8,13 +8,14 @@ https://members.orcid.org/sites/default/files/vector_iD_icon.svg https://github.com/bolorundurowb/shortid/ shortid short id databse identifier key primarykey mongodb sql - - add in options validation + - fix thread safety issues +- add in further restrictions to maintain uniqueness Bolorunduro Winner-Timothy A library that generates random identifiers from 7 to 14 characters. Identifiers generated can be used as primary keys for databases or unique identifiers. true true shortid git - 2.0.3 + 2.1.0 From f0eda9f8f6b16c4d118935379344e9c07eefb3fb Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Tue, 25 Aug 2020 12:10:39 +0100 Subject: [PATCH 2/9] ft: update the constants --- shortid/Utils/Constants.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shortid/Utils/Constants.cs b/shortid/Utils/Constants.cs index 617c3cc..648da10 100644 --- a/shortid/Utils/Constants.cs +++ b/shortid/Utils/Constants.cs @@ -2,8 +2,8 @@ namespace shortid.Utils { internal static class Constants { - public const int MinimumAutoLength = 7; + public const int MinimumAutoLength = 8; - public const int MaximumAutoLength = 15; + public const int MaximumAutoLength = 20; } } From 26898b01ad94fd7de34712cfc3b13a81dcb48b1e Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Tue, 25 Aug 2020 12:14:10 +0100 Subject: [PATCH 3/9] ft: update the char pool to remove confusing chars --- shortid/ShortId.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/shortid/ShortId.cs b/shortid/ShortId.cs index ceaad63..58fdfe0 100644 --- a/shortid/ShortId.cs +++ b/shortid/ShortId.cs @@ -1,6 +1,7 @@ using System; using System.Text; using shortid.Configuration; +using shortid.Utils; namespace shortid { @@ -8,10 +9,10 @@ public static class ShortId { // app variables private static Random _random = new Random(); - private const string Bigs = "ABCDEFGHIJKLMNOPQRSTUVWXY"; - private const string Smalls = "abcdefghjlkmnopqrstuvwxyz"; + private const string Bigs = "ABCDEFGHIJKLMNPQRSTUVWXY"; + private const string Smalls = "abcdefghjklmnopqrstuvwxyz"; private const string Numbers = "0123456789"; - private const string Specials = "-_"; + private const string Specials = "_-"; private static string _pool = $"{Smalls}{Bigs}"; // thread management variables From 83c4a7a9026b14de7e0cf82b3238c164a6a8920b Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Tue, 25 Aug 2020 12:16:08 +0100 Subject: [PATCH 4/9] ft: fix thred safety on random call --- shortid/ShortId.cs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/shortid/ShortId.cs b/shortid/ShortId.cs index 58fdfe0..8823656 100644 --- a/shortid/ShortId.cs +++ b/shortid/ShortId.cs @@ -78,7 +78,7 @@ public static string Generate(int length) /// The generation options. /// A random string. /// Thrown when options is null. - /// Thrown when options.Length is less than 7. + /// Thrown when options.Length is less than 8. public static string Generate(GenerationOptions options) { if (options == null) @@ -86,21 +86,13 @@ public static string Generate(GenerationOptions options) throw new ArgumentNullException(nameof(options)); } - if (options.Length < 7) + if (options.Length < Constants.MinimumAutoLength) { throw new ArgumentException( - $"The specified length of {options.Length} is less than the lower limit of 7."); - } - - string characterPool; - Random rand; - - lock (ThreadLock) - { - characterPool = _pool; - rand = _random; + $"The specified length of {options.Length} is less than the lower limit of {Constants.MinimumAutoLength} to avoid conflicts."); } + var characterPool = _pool; var poolBuilder = new StringBuilder(characterPool); if (options.UseNumbers) { @@ -117,8 +109,11 @@ public static string Generate(GenerationOptions options) var output = new char[options.Length]; for (var i = 0; i < options.Length; i++) { - var charIndex = rand.Next(0, pool.Length); - output[i] = pool[charIndex]; + lock (ThreadLock) + { + var charIndex = _random.Next(0, pool.Length); + output[i] = pool[charIndex]; + } } return new string(output); @@ -181,4 +176,4 @@ public static void Reset() } } } -} +} \ No newline at end of file From bec35bdbc9ef97bd9a7c9849757b88e46b582773 Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Tue, 25 Aug 2020 12:31:57 +0100 Subject: [PATCH 5/9] ft: fix test --- shortid.Test/ShortId.Tests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shortid.Test/ShortId.Tests.cs b/shortid.Test/ShortId.Tests.cs index 18a4fde..a0c695c 100644 --- a/shortid.Test/ShortId.Tests.cs +++ b/shortid.Test/ShortId.Tests.cs @@ -76,7 +76,7 @@ public void DoesNotAllowLengthsLessThan7() action .Should() .ThrowExactly() - .WithMessage("The specified length of 6 is less than the lower limit of 7."); + .WithMessage("The specified length of 6 is less than the lower limit of 8."); } [Fact] From 165c397e92746556d32b08da2d040c863883eaa8 Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Tue, 25 Aug 2020 12:33:42 +0100 Subject: [PATCH 6/9] ft: fix tests --- shortid.Test/ShortId.Tests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shortid.Test/ShortId.Tests.cs b/shortid.Test/ShortId.Tests.cs index a0c695c..852bb18 100644 --- a/shortid.Test/ShortId.Tests.cs +++ b/shortid.Test/ShortId.Tests.cs @@ -76,7 +76,7 @@ public void DoesNotAllowLengthsLessThan7() action .Should() .ThrowExactly() - .WithMessage("The specified length of 6 is less than the lower limit of 8."); + .WithMessage("The specified length of 6 is less than the lower limit of 8 to avoid conflicts."); } [Fact] From 5724e26a39ed5cd0cd5be978119a44f3ee84026f Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Tue, 25 Aug 2020 14:13:00 +0100 Subject: [PATCH 7/9] ft: add in more tests --- shortid.Test/ShortId.Tests.cs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/shortid.Test/ShortId.Tests.cs b/shortid.Test/ShortId.Tests.cs index 852bb18..275247a 100644 --- a/shortid.Test/ShortId.Tests.cs +++ b/shortid.Test/ShortId.Tests.cs @@ -69,7 +69,28 @@ public void SetSeedThrowsWhenCharacterSetIsLessThan20Characters() } [Fact] - public void DoesNotAllowLengthsLessThan7() + public void SetSeedWorksWithValidCharSet() + { + const string seed = "783ujrcuei039jhbewqoiewoiehjsbsahauwiwGHDEWIUkj4"; + Action action = () => { ShortId.SetCharacters(seed); }; + + action + .Should() + .NotThrow(); + } + + [Fact] + public void SetSeedThrowsWhenOptionsAreNull() + { + Action action = () => { ShortId.Generate(null); }; + + action + .Should() + .Throw(); + } + + [Fact] + public void DoesNotAllowLengthsLessThanEight() { Action action = () => { ShortId.Generate(6); }; From c9417638a802eb2bc63042a9c28b5671c79f290d Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Tue, 25 Aug 2020 15:01:15 +0100 Subject: [PATCH 8/9] ft: change char set limit requirements --- shortid/ShortId.cs | 6 +++--- shortid/Utils/Constants.cs | 4 +++- shortid/shortid.csproj | 5 ++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/shortid/ShortId.cs b/shortid/ShortId.cs index 6bbe4dd..c3f46a9 100644 --- a/shortid/ShortId.cs +++ b/shortid/ShortId.cs @@ -124,7 +124,7 @@ public static string Generate(GenerationOptions options) /// /// The new character set. /// Thrown when is null or empty. - /// Thrown when the new character set is less than 20 characters. + /// Thrown when the new character set is less than 50 characters. public static void SetCharacters(string characters) { if (string.IsNullOrWhiteSpace(characters)) @@ -141,10 +141,10 @@ public static void SetCharacters(string characters) } } - if (stringBuilder.Length < 20) + if (stringBuilder.Length < Constants.MinimumCharacterSetLength) { throw new InvalidOperationException( - "The replacement characters must be at least 20 letters in length and without whitespace."); + $"The replacement characters must be at least {Constants.MinimumCharacterSetLength} letters in length and without whitespace."); } lock (ThreadLock) diff --git a/shortid/Utils/Constants.cs b/shortid/Utils/Constants.cs index 648da10..67b7bbb 100644 --- a/shortid/Utils/Constants.cs +++ b/shortid/Utils/Constants.cs @@ -4,6 +4,8 @@ internal static class Constants { public const int MinimumAutoLength = 8; - public const int MaximumAutoLength = 20; + public const int MaximumAutoLength = 14; + + public const int MinimumCharacterSetLength = 50; } } diff --git a/shortid/shortid.csproj b/shortid/shortid.csproj index 9bac8ad..dfb6499 100644 --- a/shortid/shortid.csproj +++ b/shortid/shortid.csproj @@ -16,6 +16,9 @@ true shortid git - 2.1.0 + 3.0.0 + 3.0.0 + 3.0.0 + en-NG From 0c756744f1e03931e755feccc4bb33ea0eef549a Mon Sep 17 00:00:00 2001 From: Winner-Timothy Bolorunduro Date: Tue, 25 Aug 2020 15:23:07 +0100 Subject: [PATCH 9/9] ft: finalize changes to char limit --- shortid.Test/ShortId.Tests.cs | 4 ++-- shortid/ShortId.cs | 18 ++++++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/shortid.Test/ShortId.Tests.cs b/shortid.Test/ShortId.Tests.cs index 275247a..9d2e311 100644 --- a/shortid.Test/ShortId.Tests.cs +++ b/shortid.Test/ShortId.Tests.cs @@ -65,13 +65,13 @@ public void SetSeedThrowsWhenCharacterSetIsLessThan20Characters() .Should() .Throw() .WithMessage( - "The replacement characters must be at least 20 letters in length and without whitespace."); + "The replacement characters must be at least 50 letters in length and without whitespace."); } [Fact] public void SetSeedWorksWithValidCharSet() { - const string seed = "783ujrcuei039jhbewqoiewoiehjsbsahauwiwGHDEWIUkj4"; + const string seed = "ⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ①②③④⑤⑥⑦⑧⑨⑩⑪⑫"; Action action = () => { ShortId.SetCharacters(seed); }; action diff --git a/shortid/ShortId.cs b/shortid/ShortId.cs index c3f46a9..d251c3a 100644 --- a/shortid/ShortId.cs +++ b/shortid/ShortId.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Text; using shortid.Configuration; using shortid.Utils; @@ -132,16 +133,13 @@ public static void SetCharacters(string characters) throw new ArgumentException("The replacement characters must not be null or empty."); } - var stringBuilder = new StringBuilder(); - foreach (var character in characters) - { - if (!char.IsWhiteSpace(character)) - { - stringBuilder.Append(character); - } - } + var charSet = characters + .ToCharArray() + .Where(x => !char.IsWhiteSpace(x)) + .Distinct() + .ToArray(); - if (stringBuilder.Length < Constants.MinimumCharacterSetLength) + if (charSet.Length < Constants.MinimumCharacterSetLength) { throw new InvalidOperationException( $"The replacement characters must be at least {Constants.MinimumCharacterSetLength} letters in length and without whitespace."); @@ -149,7 +147,7 @@ public static void SetCharacters(string characters) lock (ThreadLock) { - _pool = stringBuilder.ToString(); + _pool = new string(charSet); } }