Skip to content

Commit

Permalink
Add Special password NanoID
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelBoucey committed Sep 25, 2023
1 parent dbd1d1c commit 856b1a5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
16 changes: 9 additions & 7 deletions .github/workflows/haskell-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#
# For more information, see https://github.com/haskell-CI/haskell-ci
#
# version: 0.16.2
# version: 0.17.20230925
#
# REGENDATA ("0.16.2",["github","NanoID.cabal"])
# REGENDATA ("0.17.20230925",["github","NanoID.cabal"])
#
name: Haskell-CI
on:
Expand Down Expand Up @@ -86,7 +86,7 @@ jobs:
apt-get install -y --no-install-recommends gnupg ca-certificates dirmngr curl git software-properties-common libtinfo5
if [ "${{ matrix.setup-method }}" = ghcup ]; then
mkdir -p "$HOME/.ghcup/bin"
curl -sL https://downloads.haskell.org/ghcup/0.1.19.2/x86_64-linux-ghcup-0.1.19.2 > "$HOME/.ghcup/bin/ghcup"
curl -sL https://downloads.haskell.org/ghcup/0.1.19.5/x86_64-linux-ghcup-0.1.19.5 > "$HOME/.ghcup/bin/ghcup"
chmod a+x "$HOME/.ghcup/bin/ghcup"
"$HOME/.ghcup/bin/ghcup" install ghc "$HCVER" || (cat "$HOME"/.ghcup/logs/*.* && false)
"$HOME/.ghcup/bin/ghcup" install cabal 3.10.1.0 || (cat "$HOME"/.ghcup/logs/*.* && false)
Expand All @@ -95,7 +95,7 @@ jobs:
apt-get update
apt-get install -y "$HCNAME"
mkdir -p "$HOME/.ghcup/bin"
curl -sL https://downloads.haskell.org/ghcup/0.1.19.2/x86_64-linux-ghcup-0.1.19.2 > "$HOME/.ghcup/bin/ghcup"
curl -sL https://downloads.haskell.org/ghcup/0.1.19.5/x86_64-linux-ghcup-0.1.19.5 > "$HOME/.ghcup/bin/ghcup"
chmod a+x "$HOME/.ghcup/bin/ghcup"
"$HOME/.ghcup/bin/ghcup" install cabal 3.10.1.0 || (cat "$HOME"/.ghcup/logs/*.* && false)
fi
Expand All @@ -111,10 +111,12 @@ jobs:
echo "CABAL_CONFIG=$HOME/.cabal/config" >> "$GITHUB_ENV"
HCDIR=/opt/$HCKIND/$HCVER
if [ "${{ matrix.setup-method }}" = ghcup ]; then
HC=$HOME/.ghcup/bin/$HCKIND-$HCVER
HC=$("$HOME/.ghcup/bin/ghcup" whereis ghc "$HCVER")
HCPKG=$(echo "$HC" | sed 's#ghc$#ghc-pkg#')
HADDOCK=$(echo "$HC" | sed 's#ghc$#haddock#')
echo "HC=$HC" >> "$GITHUB_ENV"
echo "HCPKG=$HOME/.ghcup/bin/$HCKIND-pkg-$HCVER" >> "$GITHUB_ENV"
echo "HADDOCK=$HOME/.ghcup/bin/haddock-$HCVER" >> "$GITHUB_ENV"
echo "HCPKG=$HCPKG" >> "$GITHUB_ENV"
echo "HADDOCK=$HADDOCK" >> "$GITHUB_ENV"
echo "CABAL=$HOME/.ghcup/bin/cabal-3.10.1.0 -vnormal+nowrap" >> "$GITHUB_ENV"
else
HC=$HCDIR/bin/$HCKIND
Expand Down
2 changes: 1 addition & 1 deletion NanoID.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: NanoID
version: 3.3.0.1
version: 3.4.0
synopsis: NanoID generator
description: Library and CLI tool for NanoID generation
license: BSD3
Expand Down
8 changes: 5 additions & 3 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# NanoID generator, library and CLI tool ![CI](https://github.com/MichelBoucey/NanoID/actions/workflows/haskell-ci.yml/badge.svg)
```
[user@box ~] $ nanoid -h
nanoid v3.3.0, (c) Michel Boucey 2022-2023
nanoid v3.4.0, (c) Michel Boucey 2022-2023
Usage: nanoid [-a|--alphabet ARG] [-l|--length ARG] [-p|--password]
[-q|--quantity ARG] [-n|--newline] [-v|--version]
Usage: nanoid [-a|--alphabet ARG] [-l|--length ARG] [-q|--quantity ARG]
[-n|--newline]
NanoID generator
Available options:
-a,--alphabet ARG Use an alternative alphabet (ascii chars only)
-l,--length ARG Get a shorter NanoID (Default length is 21 chars)
-p,--password Special password generation
-q,--quantity ARG Quantity of NanoID to generate
-n,--newline Do not output the trailing newline
-v,--version Show version
Expand Down
6 changes: 5 additions & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ main = do
else if quantity < 1
then strFail "quantity"
else do
let alphabet' =
if password
then specialPassword
else toAlphabet alphabet
replicateM_ quantity $
createSystemRandom
>>= customNanoID (toAlphabet alphabet) (toEnum length)
>>= customNanoID alphabet' (toEnum length)
>>= putNanoID newline
exitSuccess
where
Expand Down
6 changes: 6 additions & 0 deletions app/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ data Options =
Options
{ alphabet :: String
, length :: Int
, password :: Bool
, quantity :: Int
, newline :: Bool
, showver :: Bool
Expand Down Expand Up @@ -45,6 +46,11 @@ options =
<> long "length"
<> help "Get a shorter NanoID (Default length is 21 chars)"
<> value 21 )
<*>
flag False True
( short 'p'
<> long "password"
<> help "Special password generation" )
<*>
option auto
( short 'q'
Expand Down
16 changes: 11 additions & 5 deletions lib/Data/NanoID.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ nanoID :: GenIO -> IO NanoID
nanoID = customNanoID defaultAlphabet 21

-- | Customable 'NanoID' generator function
customNanoID :: Alphabet -- ^ An 'Alphabet' of your choice
-> Length -- ^ A 'NanoID' length (the standard length is 21 chars)
-> GenIO -- ^ The pseudo-random number generator state
-> IO NanoID
customNanoID
:: Alphabet -- ^ An 'Alphabet' of your choice
-> Length -- ^ A 'NanoID' length (the standard length is 21 chars)
-> GenIO -- ^ The pseudo-random number generator state
-> IO NanoID
customNanoID a l g =
let
ua = unAlphabet a
Expand All @@ -72,7 +73,7 @@ customNanoID a l g =
defaultAlphabet :: Alphabet
defaultAlphabet = toAlphabet "ABCDEFGHIJKLMNOPKRSTUVWXYZ_1234567890-abcdefghijklmnopqrstuvwxyz"

-- * Some predefined Alphabets, borrowed from https://github.com/CyberAP/nanoid-dictionary
-- * Some predefined 'Alphabet's, borrowed from https://github.com/CyberAP/nanoid-dictionary

numbers :: Alphabet
numbers = toAlphabet "1234567890"
Expand All @@ -98,3 +99,8 @@ nolookalikes = toAlphabet "346789ABCDEFGHJKLMNPQRTUVWXYabcdefghijkmnpqrtwxyz"
nolookalikesSafe :: Alphabet
nolookalikesSafe = toAlphabet "6789ABCDEFGHJKLMNPQRTUWYabcdefghijkmnpqrtwyz"

-- * Special password

specialPassword :: Alphabet
specialPassword = toAlphabet "67{8_9A!B>CDEF)GH=JKL(MNPQ%RTU]W.Ya@bc%def&g[hij}k<m#-npq:r+twyz"

0 comments on commit 856b1a5

Please sign in to comment.