├── go.mod ├── README.md ├── example └── example.go ├── .github ├── workflows │ ├── build-test.yml │ ├── lint-test.yml │ ├── codeql-analysis.yml │ ├── release_tag.yml │ └── autorelease-tag.yml └── dependabot.yml ├── LICENSE.md ├── blackrock_test.go └── blackrock.go /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/projectdiscovery/blackrock 2 | 3 | go 1.18 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # blackrock 2 | blackrock cipher based on masscan 3 | 4 | the cipher allows to visit all the elements of a finite space only once in a pseudo-random way -------------------------------------------------------------------------------- /example/example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/projectdiscovery/blackrock" 7 | ) 8 | 9 | func main() { 10 | test := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "h"} 11 | bl := blackrock.New(10, 5) 12 | 13 | for i := 0; i < len(test); i++ { 14 | idx := bl.Shuffle(int64(i)) 15 | fmt.Println(test[idx]) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.github/workflows/build-test.yml: -------------------------------------------------------------------------------- 1 | name: 🔨 Build Test 2 | on: 3 | push: 4 | pull_request: 5 | workflow_dispatch: 6 | 7 | 8 | jobs: 9 | build: 10 | name: Test Builds 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Set up Go 14 | uses: actions/setup-go@v4 15 | with: 16 | go-version: 1.18 17 | 18 | - name: Check out code 19 | uses: actions/checkout@v3 20 | 21 | - name: Test 22 | run: go test ./... 23 | 24 | - name: Test Example 25 | run: go run . 26 | working-directory: example/ -------------------------------------------------------------------------------- /.github/workflows/lint-test.yml: -------------------------------------------------------------------------------- 1 | name: 🙏🏻 Lint Test 2 | on: 3 | push: 4 | pull_request: 5 | workflow_dispatch: 6 | 7 | jobs: 8 | lint: 9 | name: Lint Test 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Checkout code 13 | uses: actions/checkout@v3 14 | - name: Set up Go 15 | uses: actions/setup-go@v4 16 | with: 17 | go-version: 1.18 18 | - name: Run golangci-lint 19 | uses: golangci/golangci-lint-action@v3.4.0 20 | with: 21 | version: latest 22 | args: --timeout 5m 23 | working-directory: . 24 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | name: 🚨 CodeQL Analysis 2 | 3 | on: 4 | workflow_dispatch: 5 | pull_request: 6 | branches: 7 | - dev 8 | 9 | jobs: 10 | analyze: 11 | name: Analyze 12 | runs-on: ubuntu-latest 13 | permissions: 14 | actions: read 15 | contents: read 16 | security-events: write 17 | 18 | strategy: 19 | fail-fast: false 20 | matrix: 21 | language: [ 'go' ] 22 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] 23 | 24 | steps: 25 | - name: Checkout repository 26 | uses: actions/checkout@v3 27 | 28 | # Initializes the CodeQL tools for scanning. 29 | - name: Initialize CodeQL 30 | uses: github/codeql-action/init@v2 31 | with: 32 | languages: ${{ matrix.language }} 33 | 34 | - name: Autobuild 35 | uses: github/codeql-action/autobuild@v2 36 | 37 | - name: Perform CodeQL Analysis 38 | uses: github/codeql-action/analyze@v2 -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 ProjectDiscovery 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | 9 | # Maintain dependencies for GitHub Actions 10 | - package-ecosystem: "github-actions" 11 | directory: "/" 12 | schedule: 13 | interval: "weekly" 14 | target-branch: "main" 15 | commit-message: 16 | prefix: "chore" 17 | include: "scope" 18 | 19 | # Maintain dependencies for go modules 20 | - package-ecosystem: "gomod" 21 | directory: "/" 22 | schedule: 23 | interval: "weekly" 24 | target-branch: "main" 25 | commit-message: 26 | prefix: "chore" 27 | include: "scope" 28 | 29 | # Maintain dependencies for docker 30 | - package-ecosystem: "docker" 31 | directory: "/" 32 | schedule: 33 | interval: "weekly" 34 | target-branch: "main" 35 | commit-message: 36 | prefix: "chore" 37 | include: "scope" -------------------------------------------------------------------------------- /.github/workflows/release_tag.yml: -------------------------------------------------------------------------------- 1 | name: 🔖 Release Tag 2 | 3 | on: 4 | push: 5 | workflow_dispatch: 6 | schedule: 7 | - cron: '0 0 * * 0' 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Check out code 14 | uses: actions/checkout@v3 15 | with: 16 | fetch-depth: 0 17 | 18 | - name: Get Commit Count 19 | id: get_commit 20 | run: git rev-list `git rev-list --tags --no-walk --max-count=1`..HEAD --count | xargs -I {} echo COMMIT_COUNT={} >> $GITHUB_OUTPUT 21 | 22 | - name: Create release and tag 23 | if: ${{ steps.get_commit.outputs.COMMIT_COUNT > 0 }} 24 | id: tag_version 25 | uses: mathieudutour/github-tag-action@v6.1 26 | with: 27 | github_token: ${{ secrets.GITHUB_TOKEN }} 28 | 29 | - name: Create a GitHub release 30 | if: ${{ steps.get_commit.outputs.COMMIT_COUNT > 0 }} 31 | uses: actions/create-release@v1 32 | env: 33 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 34 | with: 35 | tag_name: ${{ steps.tag_version.outputs.new_tag }} 36 | release_name: Release ${{ steps.tag_version.outputs.new_tag }} 37 | body: ${{ steps.tag_version.outputs.changelog }} -------------------------------------------------------------------------------- /.github/workflows/autorelease-tag.yml: -------------------------------------------------------------------------------- 1 | name: 🔖 Auto release gh action 2 | 3 | on: 4 | workflow_dispatch: 5 | schedule: 6 | - cron: '0 0 * * 0' 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Check out code 13 | uses: actions/checkout@v3 14 | with: 15 | fetch-depth: 0 16 | 17 | - name: Get Commit Count 18 | id: get_commit 19 | run: git rev-list `git rev-list --tags --no-walk --max-count=1`..HEAD --count | xargs -I {} echo COMMIT_COUNT={} >> $GITHUB_OUTPUT 20 | 21 | - name: Create release and tag 22 | if: ${{ steps.get_commit.outputs.COMMIT_COUNT > 0 }} 23 | id: tag_version 24 | uses: mathieudutour/github-tag-action@v6.1 25 | with: 26 | github_token: ${{ secrets.GITHUB_TOKEN }} 27 | 28 | - name: Create a GitHub release 29 | if: ${{ steps.get_commit.outputs.COMMIT_COUNT > 0 }} 30 | uses: actions/create-release@v1 31 | env: 32 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 33 | with: 34 | tag_name: ${{ steps.tag_version.outputs.new_tag }} 35 | release_name: Release ${{ steps.tag_version.outputs.new_tag }} 36 | body: ${{ steps.tag_version.outputs.changelog }} 37 | -------------------------------------------------------------------------------- /blackrock_test.go: -------------------------------------------------------------------------------- 1 | package blackrock 2 | 3 | import ( 4 | "reflect" 5 | "testing" 6 | ) 7 | 8 | func TestNew(t *testing.T) { 9 | type args struct { 10 | rangez int64 11 | seed int64 12 | } 13 | tests := []struct { 14 | name string 15 | args func(t *testing.T) args 16 | 17 | want1 *BlackRock 18 | }{ 19 | { 20 | name: "must solve the square root and increment B while the result is less than rangez", 21 | args: func(*testing.T) args { 22 | return args{ 23 | rangez: 4, 24 | seed: 1, 25 | } 26 | }, 27 | want1: &BlackRock{ 28 | Rounds: 3, 29 | Seed: 1, 30 | Range: 4, 31 | A: 1, 32 | B: 5, 33 | }, 34 | }, 35 | { 36 | name: "if split is zero the value of A must be 1", 37 | args: func(*testing.T) args { 38 | return args{ 39 | rangez: 0, 40 | seed: 1, 41 | } 42 | }, 43 | want1: &BlackRock{ 44 | Rounds: 3, 45 | Seed: 1, 46 | Range: 0, 47 | A: 1, 48 | B: 1, 49 | }, 50 | }, 51 | } 52 | 53 | for _, tt := range tests { 54 | t.Run(tt.name, func(t *testing.T) { 55 | tArgs := tt.args(t) 56 | 57 | got1 := New(tArgs.rangez, tArgs.seed) 58 | 59 | if !reflect.DeepEqual(got1, tt.want1) { 60 | t.Errorf("NewBlackRock got1 = %v, want1: %v", got1, tt.want1) 61 | } 62 | }) 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /blackrock.go: -------------------------------------------------------------------------------- 1 | package blackrock 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | // Blackrock cipher implementation from masscan 8 | 9 | type BlackRock struct { 10 | Rounds int64 11 | Seed int64 12 | Range int64 13 | A int64 14 | B int64 15 | } 16 | 17 | func New(rangez, seed int64) *BlackRock { 18 | split := int64(math.Floor(math.Sqrt(float64(rangez)))) 19 | var blackrock BlackRock 20 | blackrock.Rounds = 3 21 | blackrock.Seed = seed 22 | blackrock.Range = rangez 23 | blackrock.A = split - 1 24 | blackrock.B = split + 1 25 | 26 | if blackrock.A <= 0 { 27 | blackrock.A = 1 28 | } 29 | 30 | for blackrock.A*blackrock.B <= rangez { 31 | blackrock.B++ 32 | } 33 | 34 | return &blackrock 35 | } 36 | 37 | // Inner permutation function 38 | func (blackrock *BlackRock) F(j, r, seed int64) int64 { 39 | var primes = []int64{961752031, 982324657, 15485843, 961752031} 40 | r = (r << (r & 0x4)) + r + seed 41 | return int64(math.Abs(float64((((primes[j]*r + 25) ^ r) + j)))) 42 | } 43 | 44 | // Outer feistal construction 45 | func (blackrock *BlackRock) Fe(r, a, b, m, seed int64) int64 { 46 | var ( 47 | L, R int64 48 | j int64 49 | tmp int64 50 | ) 51 | 52 | L = m % a 53 | R = m / a 54 | 55 | for j = 1; j <= r; j++ { 56 | if j&1 == 1 { 57 | tmp = (L + blackrock.F(j, R, seed)) % a 58 | } else { 59 | tmp = (L + blackrock.F(j, R, seed)) % b 60 | } 61 | L = R 62 | R = tmp 63 | } 64 | 65 | if r&1 == 1 { 66 | return a*L + R 67 | } 68 | return a*R + L 69 | } 70 | 71 | // Outer reverse feistal construction 72 | func (blackrock *BlackRock) Unfe(r, a, b, m, seed int64) int64 { 73 | var ( 74 | L, R int64 75 | j int64 76 | tmp int64 77 | ) 78 | 79 | if r&1 == 1 { 80 | R = m % a 81 | L = m / a 82 | } else { 83 | L = m % a 84 | R = m / a 85 | } 86 | 87 | for j = r; j >= 1; j-- { 88 | if j&1 == 1 { 89 | tmp = blackrock.F(j, L, seed) 90 | if tmp > R { 91 | tmp -= -R 92 | tmp = a - (tmp % a) 93 | if tmp == a { 94 | tmp = 0 95 | } 96 | } else { 97 | tmp = R - tmp 98 | tmp %= a 99 | } 100 | } else { 101 | tmp = blackrock.F(j, L, seed) 102 | if tmp > R { 103 | tmp = (tmp - R) 104 | tmp = b - (tmp % b) 105 | if tmp == b { 106 | tmp = 0 107 | } 108 | } else { 109 | tmp = R - tmp 110 | tmp %= b 111 | } 112 | } 113 | R = L 114 | L = tmp 115 | } 116 | 117 | return a*R + L 118 | } 119 | 120 | func (blackrock *BlackRock) Shuffle(m int64) int64 { 121 | c := blackrock.Fe(blackrock.Rounds, blackrock.A, blackrock.B, m, blackrock.Seed) 122 | 123 | for c >= blackrock.Range { 124 | c = blackrock.Fe(blackrock.Rounds, blackrock.A, blackrock.B, c, blackrock.Seed) 125 | } 126 | 127 | return c 128 | } 129 | 130 | func (blackrock *BlackRock) UnShuffle(m int64) int64 { 131 | c := blackrock.Unfe(blackrock.Rounds, blackrock.A, blackrock.B, m, blackrock.Seed) 132 | for c >= blackrock.Range { 133 | c = blackrock.Unfe(blackrock.Rounds, blackrock.A, blackrock.B, c, blackrock.Seed) 134 | } 135 | 136 | return c 137 | } 138 | --------------------------------------------------------------------------------