├── settings.gradle.kts ├── .gitignore ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .editorconfig ├── renovate.json ├── .github └── workflows │ ├── build.main.kts │ ├── detekt.main.kts │ ├── build.yaml │ ├── detekt.yaml │ ├── release.yaml │ └── release.main.kts ├── src ├── jvmTest │ └── kotlin │ │ └── br │ │ └── com │ │ └── colman │ │ └── simplecpfvalidator │ │ ├── ValidCpfGenerator.kt │ │ └── CpfValidatorTest.kt └── commonMain │ └── kotlin │ └── br │ └── com │ └── colman │ └── simplecpfvalidator │ └── CpfValidator.kt ├── README.md ├── gradlew.bat ├── gradlew └── LICENSE /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | rootProject.name = "simple-cpf-validator" 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | .gradle/ 3 | .idea/ 4 | .kotlin/ 5 | kotlin-js-store/ -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoColman/SimpleCpfValidator/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [{*.kt,*.kts}] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_size = 2 7 | indent_style = space 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "config:recommended" 5 | ], 6 | "automerge": true, 7 | "major": { 8 | "automerge": false 9 | }, 10 | "commitMessagePrefix": "⬆" 11 | } 12 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /.github/workflows/build.main.kts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env kotlin 2 | @file:Repository("https://repo1.maven.org/maven2/") 3 | @file:DependsOn("io.github.typesafegithub:github-workflows-kt:3.7.0") 4 | 5 | @file:Repository("https://bindings.krzeminski.it") 6 | @file:DependsOn("actions:checkout:v5") 7 | @file:DependsOn("actions:setup-java:v5") 8 | @file:DependsOn("gradle:actions__setup-gradle:v4") 9 | 10 | 11 | import io.github.typesafegithub.workflows.actions.actions.Checkout 12 | import io.github.typesafegithub.workflows.actions.actions.SetupJava 13 | import io.github.typesafegithub.workflows.actions.gradle.ActionsSetupGradle 14 | import io.github.typesafegithub.workflows.domain.RunnerType 15 | import io.github.typesafegithub.workflows.domain.triggers.PullRequest 16 | import io.github.typesafegithub.workflows.domain.triggers.Push 17 | import io.github.typesafegithub.workflows.dsl.workflow 18 | 19 | 20 | workflow( 21 | name = "Build", 22 | on = listOf(Push(), PullRequest()), 23 | sourceFile = __FILE__ 24 | ) { 25 | job(id = "build", runsOn = RunnerType.UbuntuLatest) { 26 | uses(name = "Setup JDK", action = SetupJava(javaVersion = "22", distribution = SetupJava.Distribution.Adopt)) 27 | uses(name = "Checkout", action = Checkout()) 28 | uses(name = "Setup Gradle", action = ActionsSetupGradle()) 29 | run(name = "Run Build", command = "./gradlew build") 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /.github/workflows/detekt.main.kts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env kotlin 2 | @file:Repository("https://repo1.maven.org/maven2/") 3 | @file:DependsOn("io.github.typesafegithub:github-workflows-kt:3.7.0") 4 | 5 | @file:Repository("https://bindings.krzeminski.it") 6 | @file:DependsOn("actions:checkout:v5") 7 | @file:DependsOn("actions:setup-java:v5") 8 | @file:DependsOn("gradle:actions__setup-gradle:v4") 9 | 10 | 11 | import io.github.typesafegithub.workflows.actions.actions.Checkout 12 | import io.github.typesafegithub.workflows.actions.actions.SetupJava 13 | import io.github.typesafegithub.workflows.actions.gradle.ActionsSetupGradle 14 | import io.github.typesafegithub.workflows.domain.RunnerType 15 | import io.github.typesafegithub.workflows.domain.triggers.PullRequest 16 | import io.github.typesafegithub.workflows.domain.triggers.Push 17 | import io.github.typesafegithub.workflows.dsl.workflow 18 | 19 | 20 | workflow( 21 | name = "Detekt", 22 | on = listOf(Push(), PullRequest()), 23 | sourceFile = __FILE__ 24 | ) { 25 | job(id = "detekt", runsOn = RunnerType.UbuntuLatest) { 26 | uses(name = "Setup JDK", action = SetupJava(javaVersion = "22", distribution = SetupJava.Distribution.Adopt)) 27 | uses(name = "Checkout", action = Checkout()) 28 | uses(name = "Setup Gradle", action = ActionsSetupGradle()) 29 | 30 | run(name = "Run Detekt", command = "./gradlew detektAll") 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /.github/workflows/build.yaml: -------------------------------------------------------------------------------- 1 | # This file was generated using Kotlin DSL (.github/workflows/build.main.kts). 2 | # If you want to modify the workflow, please change the Kotlin file and regenerate this YAML file. 3 | # Generated with https://github.com/typesafegithub/github-workflows-kt 4 | 5 | name: 'Build' 6 | on: 7 | push: {} 8 | pull_request: {} 9 | jobs: 10 | check_yaml_consistency: 11 | name: 'Check YAML consistency' 12 | runs-on: 'ubuntu-latest' 13 | steps: 14 | - id: 'step-0' 15 | name: 'Check out' 16 | uses: 'actions/checkout@v4' 17 | - id: 'step-1' 18 | name: 'Execute script' 19 | run: 'rm ''.github/workflows/build.yaml'' && ''.github/workflows/build.main.kts''' 20 | - id: 'step-2' 21 | name: 'Consistency check' 22 | run: 'git diff --exit-code ''.github/workflows/build.yaml''' 23 | build: 24 | runs-on: 'ubuntu-latest' 25 | needs: 26 | - 'check_yaml_consistency' 27 | steps: 28 | - id: 'step-0' 29 | name: 'Setup JDK' 30 | uses: 'actions/setup-java@v5' 31 | with: 32 | java-version: '22' 33 | distribution: 'adopt' 34 | - id: 'step-1' 35 | name: 'Checkout' 36 | uses: 'actions/checkout@v5' 37 | - id: 'step-2' 38 | name: 'Setup Gradle' 39 | uses: 'gradle/actions/setup-gradle@v4' 40 | - id: 'step-3' 41 | name: 'Run Build' 42 | run: './gradlew build' 43 | -------------------------------------------------------------------------------- /.github/workflows/detekt.yaml: -------------------------------------------------------------------------------- 1 | # This file was generated using Kotlin DSL (.github/workflows/detekt.main.kts). 2 | # If you want to modify the workflow, please change the Kotlin file and regenerate this YAML file. 3 | # Generated with https://github.com/typesafegithub/github-workflows-kt 4 | 5 | name: 'Detekt' 6 | on: 7 | push: {} 8 | pull_request: {} 9 | jobs: 10 | check_yaml_consistency: 11 | name: 'Check YAML consistency' 12 | runs-on: 'ubuntu-latest' 13 | steps: 14 | - id: 'step-0' 15 | name: 'Check out' 16 | uses: 'actions/checkout@v4' 17 | - id: 'step-1' 18 | name: 'Execute script' 19 | run: 'rm ''.github/workflows/detekt.yaml'' && ''.github/workflows/detekt.main.kts''' 20 | - id: 'step-2' 21 | name: 'Consistency check' 22 | run: 'git diff --exit-code ''.github/workflows/detekt.yaml''' 23 | detekt: 24 | runs-on: 'ubuntu-latest' 25 | needs: 26 | - 'check_yaml_consistency' 27 | steps: 28 | - id: 'step-0' 29 | name: 'Setup JDK' 30 | uses: 'actions/setup-java@v5' 31 | with: 32 | java-version: '22' 33 | distribution: 'adopt' 34 | - id: 'step-1' 35 | name: 'Checkout' 36 | uses: 'actions/checkout@v5' 37 | - id: 'step-2' 38 | name: 'Setup Gradle' 39 | uses: 'gradle/actions/setup-gradle@v4' 40 | - id: 'step-3' 41 | name: 'Run Detekt' 42 | run: './gradlew detektAll' 43 | -------------------------------------------------------------------------------- /.github/workflows/release.yaml: -------------------------------------------------------------------------------- 1 | # This file was generated using Kotlin DSL (.github/workflows/release.main.kts). 2 | # If you want to modify the workflow, please change the Kotlin file and regenerate this YAML file. 3 | # Generated with https://github.com/typesafegithub/github-workflows-kt 4 | 5 | name: 'Release' 6 | on: 7 | push: 8 | tags: 9 | - '*' 10 | jobs: 11 | check_yaml_consistency: 12 | name: 'Check YAML consistency' 13 | runs-on: 'ubuntu-latest' 14 | steps: 15 | - id: 'step-0' 16 | name: 'Check out' 17 | uses: 'actions/checkout@v4' 18 | - id: 'step-1' 19 | name: 'Execute script' 20 | run: 'rm ''.github/workflows/release.yaml'' && ''.github/workflows/release.main.kts''' 21 | - id: 'step-2' 22 | name: 'Consistency check' 23 | run: 'git diff --exit-code ''.github/workflows/release.yaml''' 24 | release: 25 | runs-on: 'macos-latest' 26 | needs: 27 | - 'check_yaml_consistency' 28 | steps: 29 | - id: 'step-0' 30 | name: 'Setup JDK' 31 | uses: 'actions/setup-java@v5' 32 | with: 33 | java-version: '22' 34 | distribution: 'adopt' 35 | - id: 'step-1' 36 | name: 'Checkout' 37 | uses: 'actions/checkout@v5' 38 | - id: 'step-2' 39 | name: 'Setup Gradle' 40 | uses: 'gradle/actions/setup-gradle@v4' 41 | - id: 'step-3' 42 | name: 'Publish to Maven Central' 43 | env: 44 | RELEASE_VERSION: '${{ github.REF_NAME }}' 45 | ORG_GRADLE_PROJECT_mavenCentralUsername: '${{ secrets.OSSRH_USERNAME }}' 46 | ORG_GRADLE_PROJECT_mavenCentralPassword: '${{ secrets.OSSRH_PASSWORD }}' 47 | ORG_GRADLE_PROJECT_signingInMemoryKey: '${{ secrets.SIGNING_KEY }}' 48 | ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: '${{ secrets.SIGNING_PASSWORD }}' 49 | run: './gradlew publish' 50 | -------------------------------------------------------------------------------- /.github/workflows/release.main.kts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env kotlin 2 | @file:Repository("https://repo1.maven.org/maven2/") 3 | @file:DependsOn("io.github.typesafegithub:github-workflows-kt:3.7.0") 4 | 5 | @file:Repository("https://bindings.krzeminski.it") 6 | @file:DependsOn("actions:checkout:v5") 7 | @file:DependsOn("actions:setup-java:v5") 8 | @file:DependsOn("gradle:actions__setup-gradle:v4") 9 | 10 | import io.github.typesafegithub.workflows.actions.actions.Checkout 11 | import io.github.typesafegithub.workflows.actions.actions.SetupJava 12 | import io.github.typesafegithub.workflows.actions.gradle.ActionsSetupGradle 13 | import io.github.typesafegithub.workflows.domain.RunnerType 14 | import io.github.typesafegithub.workflows.domain.triggers.Push 15 | import io.github.typesafegithub.workflows.dsl.expressions.Contexts 16 | import io.github.typesafegithub.workflows.dsl.expressions.expr 17 | import io.github.typesafegithub.workflows.dsl.workflow 18 | 19 | val OSSRH_USERNAME by Contexts.secrets 20 | val OSSRH_PASSWORD by Contexts.secrets 21 | val SIGNING_KEY by Contexts.secrets 22 | val SIGNING_PASSWORD by Contexts.secrets 23 | val REF_NAME by Contexts.github 24 | 25 | 26 | workflow( 27 | name = "Release", 28 | on = listOf(Push(tags = listOf("*"))), 29 | sourceFile = __FILE__ 30 | ) { 31 | job(id = "release", runsOn = RunnerType.MacOSLatest) { 32 | uses(name = "Setup JDK", action = SetupJava(javaVersion = "22", distribution = SetupJava.Distribution.Adopt)) 33 | uses(name = "Checkout", action = Checkout()) 34 | uses(name = "Setup Gradle", action = ActionsSetupGradle()) 35 | 36 | run( 37 | name = "Publish to Maven Central", 38 | command = "./gradlew publish", 39 | env = linkedMapOf( 40 | "RELEASE_VERSION" to expr { REF_NAME }, 41 | "ORG_GRADLE_PROJECT_mavenCentralUsername" to expr { OSSRH_USERNAME }, 42 | "ORG_GRADLE_PROJECT_mavenCentralPassword" to expr { OSSRH_PASSWORD }, 43 | "ORG_GRADLE_PROJECT_signingInMemoryKey" to expr { SIGNING_KEY }, 44 | "ORG_GRADLE_PROJECT_signingInMemoryKeyPassword" to expr { SIGNING_PASSWORD } 45 | ) 46 | ) 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/jvmTest/kotlin/br/com/colman/simplecpfvalidator/ValidCpfGenerator.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Leonardo Colman Lopes 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package br.com.colman.simplecpfvalidator 17 | 18 | import io.kotest.property.Arb 19 | import io.kotest.property.arbitrary.arbitrary 20 | import io.kotest.property.arbitrary.int 21 | import io.kotest.property.arbitrary.next 22 | 23 | val ValidCpfGenerator = arbitrary( 24 | edgecases = listOf( 25 | "10147788080", 26 | "98503877007", 27 | "57773940002", 28 | "27849112091", 29 | "00000000191" 30 | ) 31 | ) { 32 | val digits = List(9) { randomDigit() } 33 | val firstVerifierDigit = digits.firstVerifierDigit() 34 | val secondVerifierDigit = digits.secondVerifierDigit(firstVerifierDigit) 35 | 36 | digits.joinToString(separator = "") + "$firstVerifierDigit" + "$secondVerifierDigit" 37 | } 38 | 39 | private fun List.firstVerifierDigit(): Int { 40 | val weights = (10 downTo 2).toList() 41 | return calculateVerifierDigit(weights, this) 42 | } 43 | 44 | private fun List.secondVerifierDigit(firstVerifierDigit: Int): Int { 45 | val weights = (11 downTo 2).toList() 46 | return calculateVerifierDigit(weights, this + firstVerifierDigit) 47 | } 48 | 49 | private fun calculateVerifierDigit(weights: List, values: List): Int { 50 | var total = 0 51 | values.forEachIndexed { index, i -> 52 | total += i * weights[index] 53 | } 54 | 55 | val divisionRemainder = total % 11 56 | return if (divisionRemainder < 2) 0 else 11 - divisionRemainder 57 | } 58 | private fun randomDigit() = Arb.int(0, 9).next() 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple Cpf Validator 2 | 3 | [![Build](https://github.com/LeoColman/SimpleCpfValidator/workflows/Build/badge.svg)](https://github.com/LeoColman/SimpleCpfValidator/actions/workflows/build.yaml) 4 | [![GitHub](https://img.shields.io/github/license/LeoColman/SimpleCpfValidator.svg)](https://github.com/LeoColman/SimpleCpfValidator/blob/master/LICENSE) 5 | [![Maven Central](https://img.shields.io/maven-central/v/br.com.colman.simplecpfvalidator/simple-cpf-validator.svg)](https://search.maven.org/search?q=g:br.com.colman.simplecpfvalidator) 6 | [![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin/tree/readme#validation-back-) 7 | [![Zero Dependencies Badge](https://img.shields.io/badge/Dependencies-0-brightgreen)](build.gradle.kts) 8 | ![Maintenance](https://img.shields.io/maintenance/yes/2025) 9 | 10 | 11 | 12 | A validação de CPF sempre existiu, mas ainda é feita de forma repetitiva em várias aplicações. O mesmo código acaba sendo copiado e colado em diferentes lugares. 13 | 14 | O Simple CPF Validator resolve esse problema ao oferecer uma validação pronta para uso, tanto em testes quanto no cadastro de usuários. Isso evita código duplicado e reduz erros no reuso. 15 | 16 | # Utilizando 17 | Usar o Simple CPF Validator é simples. Primeiro, adicione a dependência ao seu projeto no Gradle: 18 | 19 | `implementation("br.com.colman.simplecpfvalidator:simple-cpf-validator:{version}")` 20 | 21 | Depois, basta chamar a função em qualquer `String`: 22 | 23 | `"12345678911".isCpf()` 24 | 25 | Por padrão, os caracteres `.` e `-` são ignorados, permitindo o uso de formatos como `123.456.789-11`. Se precisar modificar quais caracteres devem ser removidos, use o parâmetro `charactersToIgnore`: 26 | 27 | `"123.456.789/11".isCpf(charactersToIgnore = listOf('.', '/'))` 28 | 29 | ## CPFs inválidos 30 | 31 | Os CPFs com todos os dígitos iguais (`111.111.111-11`, `222.222.222-22`, ..., `999.999.999-99`) são considerados inválidos e retornarão `false`. 32 | 33 | Já o CPF `000.000.001-91`, que teoricamente representa pessoas sem CPF, será tratado como válido por este validador. 34 | 35 | ## Contribuindo 36 | 37 | Contribuições são bem-vindas! Se tiver sugestões, abra uma _issue_ ou envie um _pull request_. 38 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | @rem SPDX-License-Identifier: Apache-2.0 17 | @rem 18 | 19 | @if "%DEBUG%"=="" @echo off 20 | @rem ########################################################################## 21 | @rem 22 | @rem Gradle startup script for Windows 23 | @rem 24 | @rem ########################################################################## 25 | 26 | @rem Set local scope for the variables with windows NT shell 27 | if "%OS%"=="Windows_NT" setlocal 28 | 29 | set DIRNAME=%~dp0 30 | if "%DIRNAME%"=="" set DIRNAME=. 31 | @rem This is normally unused 32 | set APP_BASE_NAME=%~n0 33 | set APP_HOME=%DIRNAME% 34 | 35 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 36 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 37 | 38 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 39 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 40 | 41 | @rem Find java.exe 42 | if defined JAVA_HOME goto findJavaFromJavaHome 43 | 44 | set JAVA_EXE=java.exe 45 | %JAVA_EXE% -version >NUL 2>&1 46 | if %ERRORLEVEL% equ 0 goto execute 47 | 48 | echo. 1>&2 49 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 50 | echo. 1>&2 51 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 52 | echo location of your Java installation. 1>&2 53 | 54 | goto fail 55 | 56 | :findJavaFromJavaHome 57 | set JAVA_HOME=%JAVA_HOME:"=% 58 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 59 | 60 | if exist "%JAVA_EXE%" goto execute 61 | 62 | echo. 1>&2 63 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 64 | echo. 1>&2 65 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 66 | echo location of your Java installation. 1>&2 67 | 68 | goto fail 69 | 70 | :execute 71 | @rem Setup the command line 72 | 73 | 74 | 75 | @rem Execute Gradle 76 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* 77 | 78 | :end 79 | @rem End local scope for the variables with windows NT shell 80 | if %ERRORLEVEL% equ 0 goto mainEnd 81 | 82 | :fail 83 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 84 | rem the _cmd.exe /c_ return code! 85 | set EXIT_CODE=%ERRORLEVEL% 86 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 87 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 88 | exit /b %EXIT_CODE% 89 | 90 | :mainEnd 91 | if "%OS%"=="Windows_NT" endlocal 92 | 93 | :omega 94 | -------------------------------------------------------------------------------- /src/jvmTest/kotlin/br/com/colman/simplecpfvalidator/CpfValidatorTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Leonardo Colman Lopes 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package br.com.colman.simplecpfvalidator 17 | 18 | import io.kotest.core.spec.style.FunSpec 19 | import io.kotest.inspectors.forAll 20 | import io.kotest.matchers.booleans.shouldBeFalse 21 | import io.kotest.matchers.booleans.shouldBeTrue 22 | import io.kotest.property.Arb 23 | import io.kotest.property.arbitrary.map 24 | import io.kotest.property.arbitrary.string 25 | import io.kotest.property.checkAll 26 | 27 | 28 | class CpfValidatorTest : FunSpec({ 29 | test("Should return false on invalid CPFs") { 30 | invalidCpfs.forAll { it.shouldNotBeCpf() } 31 | } 32 | 33 | test("Should return true on valid CPFs") { 34 | ValidCpfGenerator.checkAll { it.shouldBeCpf() } 35 | } 36 | 37 | test("Should return false on random strings") { 38 | Arb.string().checkAll { it.shouldNotBeCpf() } 39 | } 40 | 41 | test("Should return false on known invalid CPFs") { 42 | knownInvalidCpfs.forAll { it.shouldNotBeCpf() } 43 | } 44 | 45 | test("Should sanitize the String given replaceable characters and still return true on valid CPFs") { 46 | ValidCpfGenerator.map { "..--.$it..--." }.checkAll { it.shouldBeCpf() } 47 | } 48 | 49 | test("Shouldn't sanitize unspecified characters") { 50 | ValidCpfGenerator.map { "$it++" }.checkAll { it.shouldNotBeCpf() } 51 | } 52 | 53 | test("Should return true on valid Long typed CPF input") { 54 | 24865482385.shouldBeCpf() 55 | } 56 | 57 | test("Should return false on invalid Long typed CPF input") { 58 | 11111111111.shouldNotBeCpf() 59 | } 60 | 61 | test("Should return false on invalid length of Long typed CPF input") { 62 | 999L.shouldNotBeCpf() 63 | } 64 | }) 65 | 66 | private fun String.shouldBeCpf() { this.isCpf().shouldBeTrue() } 67 | private fun String.shouldNotBeCpf() { this.isCpf().shouldBeFalse() } 68 | private fun Long.shouldBeCpf() { this.isCpf().shouldBeTrue() } 69 | private fun Long.shouldNotBeCpf() { this.isCpf().shouldBeFalse() } 70 | 71 | 72 | private val invalidCpfs = listOf( 73 | "00000000000", 74 | "11111111111", 75 | "22222222222", 76 | "33333333333", 77 | "44444444444", 78 | "55555555555", 79 | "66666666666", 80 | "77777777777", 81 | "88888888888", 82 | "99999999999" 83 | ) 84 | 85 | // Generated some valid CPFs and changed the verification digits 86 | private val knownInvalidCpfs = listOf( 87 | "01202301204", 88 | "73681243191", 89 | "25407714320", 90 | "11438273844", 91 | "18706863061", 92 | "67358678312", 93 | "57506620392", 94 | "46637587037", 95 | "72366272830", 96 | "50763321061", 97 | "04716481248", 98 | "27736741469", 99 | "48632488652", 100 | "22700081707", 101 | "17621402840", 102 | "64010063239" 103 | ) 104 | -------------------------------------------------------------------------------- /src/commonMain/kotlin/br/com/colman/simplecpfvalidator/CpfValidator.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Leonardo Colman Lopes 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | @file:Suppress("MagicNumber") 17 | 18 | package br.com.colman.simplecpfvalidator 19 | 20 | import kotlin.math.abs 21 | 22 | /** 23 | * Verifies that this String is a CPF 24 | * 25 | * This function checks if a given string is a CPF (Cadastro de Pessoa Fisica in Portuguese), which is the Brazilian 26 | * individual taxpayer registry identification. 27 | * 28 | * All strings will first be sanitized, with [charactersToIgnore] chars removed from it (it's usual for the document 29 | * to come in the form of xxx.xxx.xxx-yy, which isn't a valid cpf number), and then they'll be validated according to 30 | * the CPF specification. 31 | * 32 | * The CPFs 111.111.111-11, 222.222.222-22, ..., 999.999.999-99 although numeric valid are considered invalid CPFs as 33 | * per the specification. 34 | * 35 | * *ATTENTION*: Although the CPF 000.000.001-91 is supposed to be used only for representing people without a CPF 36 | * document, it will be considered valid. 37 | * 38 | * @see [https://pt.wikipedia.org/wiki/Cadastro_de_pessoas_f%C3%ADsicas] 39 | * @see [http://normas.receita.fazenda.gov.br/sijut2consulta/link.action?visao=anotado&idAto=1893] 40 | */ 41 | fun String.isCpf(charactersToIgnore: List = listOf('.', '-')): Boolean { 42 | val cleanCpf = this.filterNot { it in charactersToIgnore } 43 | if (cleanCpf.containsInvalidCPFChars() || cleanCpf.isInvalidCpfSize() || cleanCpf.isInvalidCpf()) return false 44 | return cleanCpf.hasValidVerificationDigits() 45 | } 46 | 47 | /** 48 | * Verifies that this Long is a CPF 49 | * 50 | * This function checks if a given Long is a CPF (Cadastro de Pessoa Fisica in Portuguese), which is the Brazilian 51 | * individual taxpayer registry identification. 52 | * 53 | * If a negative value is used, the absolute (positive) will be considered instead. 54 | * 55 | * The CPFs 111.111.111-11, 222.222.222-22, ..., 999.999.999-99 although numeric valid are considered invalid CPFs as 56 | * per the specification. 57 | * 58 | * *ATTENTION*: Although the CPF 000.000.001-91 is supposed to be used only for representing people without a CPF 59 | * document, it will be considered valid. 60 | * 61 | * @see [https://pt.wikipedia.org/wiki/Cadastro_de_pessoas_f%C3%ADsicas] 62 | * @see [http://normas.receita.fazenda.gov.br/sijut2consulta/link.action?visao=anotado&idAto=1893] 63 | */ 64 | fun Long.isCpf(): Boolean { 65 | val absNumber = abs(this) 66 | return absNumber.toString().isCpf() 67 | } 68 | 69 | private fun String.containsInvalidCPFChars() = this.any { !it.isDigit() } 70 | private fun String.isInvalidCpfSize() = this.length != 11 71 | private fun String.isInvalidCpf() = this in invalidCpfs 72 | 73 | // Algorithm from https://www.somatematica.com.br/faq/cpf.php 74 | private fun String.hasValidVerificationDigits(): Boolean { 75 | val firstNineDigits = substring(0..8) 76 | val digits = substring(9..10) 77 | 78 | return firstNineDigits.calculateDigits() == digits 79 | } 80 | 81 | private fun String.calculateDigits(): String { 82 | val numbers = map { it.toString().toInt() } 83 | val firstDigit = numbers.calculateFirstVerificationDigit() 84 | val secondDigit = numbers.calculateSecondVerificationDigit(firstDigit) 85 | 86 | return "$firstDigit$secondDigit" 87 | } 88 | 89 | private fun List.calculateFirstVerificationDigit(): Int { 90 | /* Given 9 CPF numbers, the first digit calculation works this way: 91 | 92 | There is a weight associated to each number index: 93 | CPF first nine digits - | A | B | C | D | E | F | G | H | I | 94 | CPF index multiplier - | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 95 | 96 | We will then sum all the digits with their multiplier: A * 10 + B * 9 + C * 8 ... 97 | With that result, the first verifier digit will be calculated with the remainder of (SUM / 11) 98 | If the remainder is 0 or 1, the digit is ZERO. If it's >=2, the digit is (11 - remainder) 99 | */ 100 | val firstNineDigits = this 101 | val weights = (10 downTo 2).toList() 102 | val sum = firstNineDigits.withIndex().sumOf { (index, element) -> weights[index] * element } 103 | 104 | val remainder = sum % 11 105 | return if (remainder < 2) 0 else 11 - remainder 106 | } 107 | 108 | private fun List.calculateSecondVerificationDigit(firstDigit: Int): Int { 109 | /* 110 | In a similar way to calculating the first digit, the second digit also works with a table of weights and the numbers 111 | 112 | However, the last digit is added to the digits 113 | 114 | CPF first nine digits + first verification digit - | A | B | C | D | E | F | G | H | I | 1st v.d. | 115 | CPF Index multiplier - | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 116 | 117 | And in the same fashion, the sum will be calculated as A * 11 + B * 10 + ... + 1st vd * 2 118 | The second verification digit uses the same formula: 119 | remainder = (SUM / 11) 120 | 2nd digit = ZERO if remainder is 0 or 1, 11 - remainder otherwise 121 | */ 122 | 123 | val firstTenDigits = this + firstDigit 124 | val weights = (11 downTo 2).toList() 125 | val sum = firstTenDigits.withIndex().sumOf { (index, element) -> weights[index] * element } 126 | 127 | val remainder = sum % 11 128 | return if (remainder < 2) 0 else 11 - remainder 129 | } 130 | 131 | /** 132 | * These CPFs although are numerically valid (i.e. [hasValidVerificationDigits]) are considered invalid as per CPF 133 | * specification 134 | */ 135 | private val invalidCpfs = listOf( 136 | "00000000000", 137 | "11111111111", 138 | "22222222222", 139 | "33333333333", 140 | "44444444444", 141 | "55555555555", 142 | "66666666666", 143 | "77777777777", 144 | "88888888888", 145 | "99999999999" 146 | ) 147 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | # SPDX-License-Identifier: Apache-2.0 19 | # 20 | 21 | ############################################################################## 22 | # 23 | # Gradle start up script for POSIX generated by Gradle. 24 | # 25 | # Important for running: 26 | # 27 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 28 | # noncompliant, but you have some other compliant shell such as ksh or 29 | # bash, then to run this script, type that shell name before the whole 30 | # command line, like: 31 | # 32 | # ksh Gradle 33 | # 34 | # Busybox and similar reduced shells will NOT work, because this script 35 | # requires all of these POSIX shell features: 36 | # * functions; 37 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 38 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 39 | # * compound commands having a testable exit status, especially «case»; 40 | # * various built-in commands including «command», «set», and «ulimit». 41 | # 42 | # Important for patching: 43 | # 44 | # (2) This script targets any POSIX shell, so it avoids extensions provided 45 | # by Bash, Ksh, etc; in particular arrays are avoided. 46 | # 47 | # The "traditional" practice of packing multiple parameters into a 48 | # space-separated string is a well documented source of bugs and security 49 | # problems, so this is (mostly) avoided, by progressively accumulating 50 | # options in "$@", and eventually passing that to Java. 51 | # 52 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 53 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 54 | # see the in-line comments for details. 55 | # 56 | # There are tweaks for specific operating systems such as AIX, CygWin, 57 | # Darwin, MinGW, and NonStop. 58 | # 59 | # (3) This script is generated from the Groovy template 60 | # https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 61 | # within the Gradle project. 62 | # 63 | # You can find Gradle at https://github.com/gradle/gradle/. 64 | # 65 | ############################################################################## 66 | 67 | # Attempt to set APP_HOME 68 | 69 | # Resolve links: $0 may be a link 70 | app_path=$0 71 | 72 | # Need this for daisy-chained symlinks. 73 | while 74 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 75 | [ -h "$app_path" ] 76 | do 77 | ls=$( ls -ld "$app_path" ) 78 | link=${ls#*' -> '} 79 | case $link in #( 80 | /*) app_path=$link ;; #( 81 | *) app_path=$APP_HOME$link ;; 82 | esac 83 | done 84 | 85 | # This is normally unused 86 | # shellcheck disable=SC2034 87 | APP_BASE_NAME=${0##*/} 88 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 89 | APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | 118 | 119 | # Determine the Java command to use to start the JVM. 120 | if [ -n "$JAVA_HOME" ] ; then 121 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 122 | # IBM's JDK on AIX uses strange locations for the executables 123 | JAVACMD=$JAVA_HOME/jre/sh/java 124 | else 125 | JAVACMD=$JAVA_HOME/bin/java 126 | fi 127 | if [ ! -x "$JAVACMD" ] ; then 128 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 129 | 130 | Please set the JAVA_HOME variable in your environment to match the 131 | location of your Java installation." 132 | fi 133 | else 134 | JAVACMD=java 135 | if ! command -v java >/dev/null 2>&1 136 | then 137 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 138 | 139 | Please set the JAVA_HOME variable in your environment to match the 140 | location of your Java installation." 141 | fi 142 | fi 143 | 144 | # Increase the maximum file descriptors if we can. 145 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 146 | case $MAX_FD in #( 147 | max*) 148 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 149 | # shellcheck disable=SC2039,SC3045 150 | MAX_FD=$( ulimit -H -n ) || 151 | warn "Could not query maximum file descriptor limit" 152 | esac 153 | case $MAX_FD in #( 154 | '' | soft) :;; #( 155 | *) 156 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 157 | # shellcheck disable=SC2039,SC3045 158 | ulimit -n "$MAX_FD" || 159 | warn "Could not set maximum file descriptor limit to $MAX_FD" 160 | esac 161 | fi 162 | 163 | # Collect all arguments for the java command, stacking in reverse order: 164 | # * args from the command line 165 | # * the main class name 166 | # * -classpath 167 | # * -D...appname settings 168 | # * --module-path (only if needed) 169 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 170 | 171 | # For Cygwin or MSYS, switch paths to Windows format before running java 172 | if "$cygwin" || "$msys" ; then 173 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 174 | 175 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 176 | 177 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 178 | for arg do 179 | if 180 | case $arg in #( 181 | -*) false ;; # don't mess with options #( 182 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 183 | [ -e "$t" ] ;; #( 184 | *) false ;; 185 | esac 186 | then 187 | arg=$( cygpath --path --ignore --mixed "$arg" ) 188 | fi 189 | # Roll the args list around exactly as many times as the number of 190 | # args, so each arg winds up back in the position where it started, but 191 | # possibly modified. 192 | # 193 | # NB: a `for` loop captures its iteration list before it begins, so 194 | # changing the positional parameters here affects neither the number of 195 | # iterations, nor the values presented in `arg`. 196 | shift # remove old arg 197 | set -- "$@" "$arg" # push replacement arg 198 | done 199 | fi 200 | 201 | 202 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 203 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 204 | 205 | # Collect all arguments for the java command: 206 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 207 | # and any embedded shellness will be escaped. 208 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 209 | # treated as '${Hostname}' itself on the command line. 210 | 211 | set -- \ 212 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 213 | -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ 214 | "$@" 215 | 216 | # Stop when "xargs" is not available. 217 | if ! command -v xargs >/dev/null 2>&1 218 | then 219 | die "xargs is not available" 220 | fi 221 | 222 | # Use "xargs" to parse quoted args. 223 | # 224 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 225 | # 226 | # In Bash we could simply go: 227 | # 228 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 229 | # set -- "${ARGS[@]}" "$@" 230 | # 231 | # but POSIX shell has neither arrays nor command substitution, so instead we 232 | # post-process each arg (as a line of input to sed) to backslash-escape any 233 | # character that might be a shell metacharacter, then use eval to reverse 234 | # that process (while maintaining the separation between arguments), and wrap 235 | # the whole thing up as a single "set" statement. 236 | # 237 | # This will of course break if any of these variables contains a newline or 238 | # an unmatched quote. 239 | # 240 | 241 | eval "set -- $( 242 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 243 | xargs -n1 | 244 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 245 | tr '\n' ' ' 246 | )" '"$@"' 247 | 248 | exec "$JAVACMD" "$@" 249 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------