Skip to content

Commit

Permalink
First public version
Browse files Browse the repository at this point in the history
  • Loading branch information
catap committed Jul 11, 2021
0 parents commit 1130c3b
Show file tree
Hide file tree
Showing 16 changed files with 1,293 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/scala.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Scala CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

env:
JAVA_OPTS: "-XX:+UseG1GC -Xmx6g"

steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Check headers
run: sbt +headerCheck
- name: Check scalafmt
run: sbt +scalafmtCheckAll
- name: Run tests
run: sbt +test
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# scala
*.class
*.log

# sbt specific
.cache
.history
.lib/
dist/*
target/
lib_managed/
src_managed/
project/boot/
project/plugins/project/
.bsp/

# IDEA stuff
.idea/

# scala-native
lowered.hnir

# dotty site
_site
6 changes: 6 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version = 2.7.5

continuationIndent.callSite = 2
continuationIndent.defnSite = 2

docstrings.style = Asterisk
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

All notable changes to this project will be documented in this file.

## [unreleased]

## [1.0.0] - 2021-07-11
- The first public release as dedicated project.

[unreleased]: https://github.com/catap/scala-blake3/compare/v1.0.0...HEAD
[1.0.0]: https://github.com/catap/scala-blake3/releases/tag/v1.0.0

125 changes: 125 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
This work is released into the public domain with CC0 1.0.

-------------------------------------------------------------------------------

Creative Commons Legal Code

CC0 1.0 Universal

CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
HEREUNDER.

Statement of Purpose

The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator
and subsequent owner(s) (each and all, an "owner") of an original work of
authorship and/or a database (each, a "Work").

Certain owners wish to permanently relinquish those rights to a Work for
the purpose of contributing to a commons of creative, cultural and
scientific works ("Commons") that the public can reliably and without fear
of later claims of infringement build upon, modify, incorporate in other
works, reuse and redistribute as freely as possible in any form whatsoever
and for any purposes, including without limitation commercial purposes.
These owners may contribute to the Commons to promote the ideal of a free
culture and the further production of creative, cultural and scientific
works, or to gain reputation or greater distribution for their Work in
part through the use and efforts of others.

For these and/or other purposes and motivations, and without any
expectation of additional consideration or compensation, the person
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
is an owner of Copyright and Related Rights in the Work, voluntarily
elects to apply CC0 to the Work and publicly distribute the Work under its
terms, with knowledge of his or her Copyright and Related Rights in the
Work and the meaning and intended legal effect of CC0 on those rights.

1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights ("Copyright and
Related Rights"). Copyright and Related Rights include, but are not
limited to, the following:

i. the right to reproduce, adapt, distribute, perform, display,
communicate, and translate a Work;
ii. moral rights retained by the original author(s) and/or performer(s);
iii. publicity and privacy rights pertaining to a person's image or
likeness depicted in a Work;
iv. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(a), below;
v. rights protecting the extraction, dissemination, use and reuse of data
in a Work;
vi. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation
thereof, including any amended or successor version of such
directive); and
vii. other similar, equivalent or corresponding rights throughout the
world based on applicable law or treaty, and any national
implementations thereof.

2. Waiver. To the greatest extent permitted by, but not in contravention
of, applicable law, Affirmer hereby overtly, fully, permanently,
irrevocably and unconditionally waives, abandons, and surrenders all of
Affirmer's Copyright and Related Rights and associated claims and causes
of action, whether now known or unknown (including existing as well as
future claims and causes of action), in the Work (i) in all territories
worldwide, (ii) for the maximum duration provided by applicable law or
treaty (including future time extensions), (iii) in any current or future
medium and for any number of copies, and (iv) for any purpose whatsoever,
including without limitation commercial, advertising or promotional
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
member of the public at large and to the detriment of Affirmer's heirs and
successors, fully intending that such Waiver shall not be subject to
revocation, rescission, cancellation, termination, or any other legal or
equitable action to disrupt the quiet enjoyment of the Work by the public
as contemplated by Affirmer's express Statement of Purpose.

3. Public License Fallback. Should any part of the Waiver for any reason
be judged legally invalid or ineffective under applicable law, then the
Waiver shall be preserved to the maximum extent permitted taking into
account Affirmer's express Statement of Purpose. In addition, to the
extent the Waiver is so judged Affirmer hereby grants to each affected
person a royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmer's Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future
time extensions), (iii) in any current or future medium and for any number
of copies, and (iv) for any purpose whatsoever, including without
limitation commercial, advertising or promotional purposes (the
"License"). The License shall be deemed effective as of the date CC0 was
applied by Affirmer to the Work. Should any part of the License for any
reason be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the remainder
of the License, and in such case Affirmer hereby affirms that he or she
will not (i) exercise any of his or her remaining Copyright and Related
Rights in the Work or (ii) assert any associated claims and causes of
action with respect to the Work, in either case contrary to Affirmer's
express Statement of Purpose.

4. Limitations and Disclaimers.

a. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
b. Affirmer offers the Work as-is and makes no representations or
warranties of any kind concerning the Work, express, implied,
statutory or otherwise, including without limitation warranties of
title, merchantability, fitness for a particular purpose, non
infringement, or the absence of latent or other defects, accuracy, or
the present or absence of errors, whether or not discoverable, all to
the greatest extent permissible under applicable law.
c. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any person's Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the
Work.
d. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to
this CC0 or use of the Work.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SHA and Shake for scala

This is Secure Hash Algorithms family which is implemented for scala, scala-js and scala-native,
without any dependencies.

This code base implements SHA-0, SHA-1, SHA-2 and SHA-3. Keep in mind that SHA-0 is broken
and I've implemented it just for fun :)

This code wasn't designed to be as fast as possible. I tried my best to make implementation fast when I did it,
but I haven't done any benchmarks yet. Will I make it one day faster or oriented to huge chunks? Probably.

The propose of this code to be fast enough to hash something up to a few megabytes at the worst case
and at few kilobytes as usual case. Where? At any scala-based application where hashing isn't the bottleneck.

This assumption also included inside API: it consumes whole provided array, it allocates and copies a lot.

If you need fast and secure hash function for scala I suggest to use [blak3](https://github.com/catap/scala-blake3).

The latest version is ![maven-central]

API is pretty simple and quite limited :)
```
scala> import ky.korins.sha._
scala> Sha2_256.hash("abc".getBytes())
val res1: Array[Byte] = Array(-70, 120, 22, -65, -113, 1, -49, -22, 65, 65, 64, -34, 93, -82, 34, 35, -80, 3, 97, -93, -106, 23, 122, -100, -76, 16, -1, 97, -14, 0, 21, -83)
scala>
```

[maven-central]: https://img.shields.io/maven-central/v/ky.korins/sha_2.13?style=flat-square
61 changes: 61 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import sbt.Keys.crossScalaVersions
import sbtcrossproject.CrossPlugin.autoImport.crossProject

lazy val scala210 = "2.10.7"
lazy val scala211 = "2.11.12"
lazy val scala212 = "2.12.14"
lazy val scala213 = "2.13.6"
lazy val scala3 = "3.0.1"

lazy val scalatestVersion = "3.2.9"

name := "sha"
ThisBuild / organization := "ky.korins"

ThisBuild / dynverSeparator := "-"

ThisBuild / scalaVersion := scala213
ThisBuild / crossScalaVersions := Seq()

ThisBuild / scalacOptions ++= Seq(
"-target:jvm-1.8",
"-unchecked",
"-deprecation"
)

ThisBuild / publishTo := sonatypePublishToBundle.value

headerLicense := LicenseDefinition.template

lazy val sha = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.crossType(CrossType.Full)
.enablePlugins(BuildInfoPlugin)
.enablePlugins(AutomateHeaderPlugin)
.in(file("."))
.settings(
publish / skip := false,
Test / publishArtifact := false,
buildInfoKeys := Seq(
BuildInfoKey.action("commit") {
scala.sys.process.Process("git rev-parse HEAD").!!.trim
}
),
headerLicense := LicenseDefinition.template,
buildInfoPackage := "ky.korins.sha",
libraryDependencies ++= Seq(
"org.scalatest" %%% "scalatest" % scalatestVersion % Test,
)
)
.jvmSettings(
scalaVersion := scala213,
crossScalaVersions := Seq(scala210, scala211, scala212, scala213, scala3)
)
.jsSettings(
scalaVersion := scala213,
crossScalaVersions := Seq(scala211, scala212, scala213, scala3),
)
.nativeSettings(
scalaVersion := scala213,
crossScalaVersions := Seq(scala211, scala212, scala213),
nativeLinkStubs := true,
)
25 changes: 25 additions & 0 deletions project/LicenseDefinition.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import de.heikoseeberger.sbtheader.HeaderPlugin.autoImport.HeaderLicense
import de.heikoseeberger.sbtheader.License
import sbt.url

object LicenseDefinition {
val template: Option[License.Custom] = Some(
HeaderLicense.Custom(
"""scala-sha - Secure Hash Algorithms family for scala, scala-js and scala-native.
|
|Written in 2020, 2021 by Kirill A. Korinsky <[email protected]>
|
|This work is released into the public domain with CC0 1.0.
|""".stripMargin
)
)

val licenses = Seq(
"CC0 1.0 Universal" -> url(
"https://github.com/catap/scala-sha/blob/master/LICENSE.txt"
),
"Apache License 2.0" -> url(
"https://github.com/catap/scala-sha/blob/master/LICENSE.txt"
)
)
}
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.5.4
18 changes: 18 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.1.0")
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.1.0")

val scalaJSVersion =
Option(System.getenv("SCALAJS_VERSION")).getOrElse("1.6.0")
val scalaNativeJSVersion =
Option(System.getenv("SCALANATIVE_VERSION")).getOrElse("0.4.0")

addSbtPlugin("org.scala-js" % "sbt-scalajs" % scalaJSVersion)
addSbtPlugin("org.scala-native" % "sbt-scala-native" % scalaNativeJSVersion)

addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.7")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.10.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.0.0")
addSbtPlugin("com.dwijnand" % "sbt-dynver" % "4.1.1")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.3")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.6.0")
56 changes: 56 additions & 0 deletions shared/src/main/scala/ky/korins/sha/Paddings.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* scala-sha - Secure Hash Algorithms family for scala, scala-js and scala-native.
*
* Written in 2020, 2021 by Kirill A. Korinsky <[email protected]>
*
* This work is released into the public domain with CC0 1.0.
*/

package ky.korins.sha

private[sha] object Paddings {
def padding_32(message: Array[Byte]): Array[Byte] = {
val origLength = message.length
val tailLength = origLength & 0x3f
val padLength = if (tailLength < 56) 64 - tailLength else 128 - tailLength

val thePad = new Array[Byte](padLength)
thePad(0) = 0x80.toByte

val lengthInBits = origLength.toLong * 8
var i = 0
while (i < 8) {
thePad(thePad.length - 1 - i) = ((lengthInBits >>> (8 * i)) & 0xff).toByte
i += 1
}

val padded = new Array[Byte](origLength + padLength)
System.arraycopy(message, 0, padded, 0, origLength)
System.arraycopy(thePad, 0, padded, origLength, thePad.length)

padded
}

def padding_64(message: Array[Byte]): Array[Byte] = {
val origLength = message.length
val tailLength = origLength & 0x7f
val padLength = if (tailLength < 112) 128 - tailLength else 256 - tailLength

val thePad = new Array[Byte](padLength)
thePad(0) = 0x80.toByte

val lengthInBits = origLength.toLong * 8
var i = 0
while (i < 8) {
thePad(thePad.length - 1 - i) = ((lengthInBits >>> (8 * i)) & 0xff).toByte
i += 1
}

val padded = new Array[Byte](origLength + padLength)
System.arraycopy(message, 0, padded, 0, origLength)
System.arraycopy(thePad, 0, padded, origLength, thePad.length)

padded
}

}
Loading

0 comments on commit 1130c3b

Please sign in to comment.