Skip to content

Commit

Permalink
Imrpoved code and clean-up
Browse files Browse the repository at this point in the history
License change to MIT
  • Loading branch information
Mobilpadde committed Apr 28, 2022
1 parent 3603ea8 commit a69ef5f
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 184 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
25 changes: 4 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
This is free and unencumbered software released into the public domain.
Copyright 2022 Mads Cordes

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
51 changes: 33 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
# moths

> e**mo**ji au**ths**
> e**mo**jicon au**th**entication**s**
# what
## what

Emojies as TOTP
Emojies as OTP (2FA)

# why
## why

Becasue why not?
Because why not?

# how
## how

Runnig this is quite easy, just run the command:
Runnig this is quite easy.

Download the dependencies:
- Download the dependencies

```sh
go mod download
```
```sh
go mod download
```

Next-up start the program
- Make a secret

```sh
go run .
```
```sh
echo -n "MOTHS_SECRET=" > .env; echo 'moths' | sha256sum | base64 | head -c 32 >> .env
```

- Start the program

```sh
go run .
```

As I said, easy-peasy!

# sample
## sample

![four generated OTP's](./data/sample.png)

<details>
<summary>v0.1</summary>

A sample out-put might be `😻🙀😺🙀🙀` which would equal the `920811` TOTP-token. Using the super secret secret of `😻` - genereated at `2022/04/28 02:05:42 AM` and a `5`-second interval.

Can be checked out at [`v0.1`](https://github.com/Mobilpadde/moths/tree/v0.1)

A sample out-put might be `😻🙀😺🙀🙀` which would equal the `920811` TOTP-token. Using the super secret secret specified in [`./secret.pem`](./secret.pem) - genereated at `2022/04/28 02:05:42` and a `5`-second interval.
</details>

# shoutout
## shoutout

I couldn't have done it without the lovely OSS's listed below:

Expand Down
Binary file added data/sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 0 additions & 65 deletions generator.go

This file was deleted.

52 changes: 0 additions & 52 deletions hotp.go

This file was deleted.

52 changes: 25 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
package main

import (
"io/ioutil"
"log"
"moths/moths"
"os"
"strings"
"time"

"github.com/enescakir/emoji"
)

func main() {
log.SetFlags(log.Llongfile | log.LstdFlags)

data, err := ioutil.ReadFile("secret.pem")
if err != nil {
log.Fatalln(err)
}
go func() {
interval := 5

secret := os.Getenv("MOTHS_SECRET")
gen, err := moths.NewMoths(secret, interval)
if err != nil {
log.Fatalln(err)
}

secret := string(data)
interval := time.Now().Unix() / 5
ticker := time.NewTicker(time.Second * time.Duration(interval))
for {
select {
case <-ticker.C:
moth, err := gen.Next(6, moths.ALPHABET_CATSDOG)
if err != nil {
log.Fatalln(err)
}

otp := GetHOTPToken(secret, interval)
moth, err := MothsGenerator(
otp,
5,
emoji.GrinningCat.String(),
emoji.GrinningCatWithSmilingEyes.String(),
emoji.CatWithTearsOfJoy.String(),
emoji.SmilingCatWithHeartEyes.String(),
emoji.CatWithWrySmile.String(),
emoji.KissingCat.String(),
emoji.WearyCat.String(),
emoji.CryingCat.String(),
emoji.PoutingCat.String(),
)
if err != nil {
log.Fatalln(err)
}
joint := strings.Join(strings.Split(moth, ""), " ")
log.Printf("moth-otp: %s\n", joint)
}
}
}()

log.Printf("moth-otp: %s => %s\n", moth, otp)
select {}
}
21 changes: 21 additions & 0 deletions moths/buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package moths

import (
"encoding/binary"
"fmt"
"math/rand"
)

func randomBufferFromHOTP(otp string) func(step int) ([]byte, error) {
buf := []byte(fmt.Sprintf("%08s", otp))
seed := int64(binary.BigEndian.Uint64(buf))
r := rand.New(rand.NewSource(seed))

return func(step int) ([]byte, error) {
buffer := make([]byte, step)
if _, err := r.Read(buffer); err != nil {
return nil, err
}
return buffer, nil
}
}
24 changes: 24 additions & 0 deletions moths/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package moths

import "github.com/enescakir/emoji"

var (
ALPHABET_CATSDOG = []string{
emoji.GrinningCat.String(),
emoji.GrinningCatWithSmilingEyes.String(),
emoji.CatWithTearsOfJoy.String(),
emoji.SmilingCatWithHeartEyes.String(),
emoji.CatWithWrySmile.String(),
emoji.KissingCat.String(),
emoji.WearyCat.String(),
emoji.CryingCat.String(),
emoji.PoutingCat.String(),
emoji.DogFace.String(),
}
)

type Moths struct {
secret string
interval int
alphabet []string
}
49 changes: 49 additions & 0 deletions moths/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package moths

import (
"errors"
"math"
"math/bits"
"strings"
)

const emojiBytes = 4

func (m *Moths) Next(numEmojies int, alphabet []string) (string, error) {
if len(alphabet) < 1 {
return "", errors.New("alphabet must be at least 1 character")
}

size := emojiBytes * numEmojies
// https://github.com/tilaklodha/google-authenticator
mask := 2<<uint32(31-bits.LeadingZeros32(uint32(len(alphabet)-1|1))) - 1
step := int(math.Ceil(1.6 * float64(mask*size) / float64(len(alphabet))))

id := new(strings.Builder)
id.Grow(size)

tkn, err := m.getHOTPToken()
if err != nil {
return "", err
}

buffer := randomBufferFromHOTP(tkn)
for {
randomBuffer, err := buffer(step)
if err != nil {
return "", err
}

for i := 0; i < step; i++ {
currentIndex := int(randomBuffer[i]) & mask

if currentIndex < len(alphabet) {
if _, err := id.WriteString(alphabet[currentIndex]); err != nil {
return "", err
} else if id.Len() == size {
return id.String(), nil
}
}
}
}
}
Loading

0 comments on commit a69ef5f

Please sign in to comment.