Skip to content

Commit

Permalink
Add TypeScript typings (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: Kasper Isager Dalsgarð <[email protected]>
  • Loading branch information
yassernasc and kasperisager authored Jan 15, 2025
1 parent 63cfc33 commit 7d9631a
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 132 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
144 changes: 72 additions & 72 deletions .github/workflows/prebuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,82 @@ jobs:
strategy:
matrix:
include:
- os: ubuntu-22.04
platform: linux
arch: x64
- os: ubuntu-22.04-arm64
platform: linux
arch: arm64
- os: ubuntu-22.04
platform: android
arch: x64
flags: -D ANDROID_STL=c++_shared
- os: ubuntu-22.04
platform: android
arch: ia32
flags: -D ANDROID_STL=c++_shared
- os: ubuntu-22.04
platform: android
arch: arm64
flags: -D ANDROID_STL=c++_shared
- os: ubuntu-22.04
platform: android
arch: arm
flags: -D ANDROID_STL=c++_shared
- os: macos-14
platform: darwin
arch: x64
- os: macos-14
platform: darwin
arch: arm64
- os: macos-14
platform: ios
arch: arm64
- os: macos-14
platform: ios
arch: arm64
tags: -simulator
flags: --simulator
- os: macos-14
platform: ios
arch: x64
tags: -simulator
flags: --simulator
- os: windows-2022
platform: win32
arch: x64
- os: windows-2022
platform: win32
arch: arm64
- os: ubuntu-22.04
platform: linux
arch: x64
- os: ubuntu-22.04-arm64
platform: linux
arch: arm64
- os: ubuntu-22.04
platform: android
arch: x64
flags: -D ANDROID_STL=c++_shared
- os: ubuntu-22.04
platform: android
arch: ia32
flags: -D ANDROID_STL=c++_shared
- os: ubuntu-22.04
platform: android
arch: arm64
flags: -D ANDROID_STL=c++_shared
- os: ubuntu-22.04
platform: android
arch: arm
flags: -D ANDROID_STL=c++_shared
- os: macos-14
platform: darwin
arch: x64
- os: macos-14
platform: darwin
arch: arm64
- os: macos-14
platform: ios
arch: arm64
- os: macos-14
platform: ios
arch: arm64
tags: -simulator
flags: --simulator
- os: macos-14
platform: ios
arch: x64
tags: -simulator
flags: --simulator
- os: windows-2022
platform: win32
arch: x64
- os: windows-2022
platform: win32
arch: arm64
runs-on: ${{ matrix.os }}
name: ${{ matrix.platform }}-${{ matrix.arch }}${{ matrix.tags }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
cache: false
go-version: stable
- uses: actions/setup-node@v4
with:
node-version: lts/*
- run: npm install -g bare-runtime bare-make
- run: npm install
- run: bare-make generate --platform ${{ matrix.platform }} --arch ${{ matrix.arch }} ${{ matrix.flags }}
- run: bare-make build
- run: bare-make install
- uses: actions/upload-artifact@v4
with:
name: ${{ matrix.platform }}-${{ matrix.arch }}${{ matrix.tags }}
path: prebuilds/*
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
cache: false
go-version: stable
- uses: actions/setup-node@v4
with:
node-version: lts/*
- run: npm install -g bare-runtime bare-make
- run: npm install
- run: bare-make generate --platform ${{ matrix.platform }} --arch ${{ matrix.arch }} ${{ matrix.flags }}
- run: bare-make build
- run: bare-make install
- uses: actions/upload-artifact@v4
with:
name: ${{ matrix.platform }}-${{ matrix.arch }}${{ matrix.tags }}
path: prebuilds/*
merge:
runs-on: ubuntu-latest
needs: prebuild
steps:
- uses: actions/download-artifact@v4
with:
path: prebuilds
merge-multiple: true
- uses: actions/upload-artifact@v4
with:
name: prebuilds
path: prebuilds
- uses: actions/download-artifact@v4
with:
path: prebuilds
merge-multiple: true
- uses: actions/upload-artifact@v4
with:
name: prebuilds
path: prebuilds
46 changes: 23 additions & 23 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ jobs:
strategy:
matrix:
include:
- os: ubuntu-latest
platform: linux
arch: x64
- os: macos-latest
platform: darwin
arch: arm64
- os: windows-latest
platform: win32
arch: x64
- os: ubuntu-latest
platform: linux
arch: x64
- os: macos-latest
platform: darwin
arch: arm64
- os: windows-latest
platform: win32
arch: x64
runs-on: ${{ matrix.os }}
name: ${{ matrix.platform }}-${{ matrix.arch }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
cache: false
go-version: stable
- uses: actions/setup-node@v4
with:
node-version: lts/*
- run: npm install -g bare-runtime bare-make
- run: npm install
- run: bare-make generate --platform ${{ matrix.platform }} --arch ${{ matrix.arch }} --debug
- run: bare-make build
- run: bare-make install
- run: npm test
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
cache: false
go-version: stable
- uses: actions/setup-node@v4
with:
node-version: lts/*
- run: npm install -g bare-runtime bare-make
- run: npm install
- run: bare-make generate --platform ${{ matrix.platform }} --arch ${{ matrix.arch }} --debug
- run: bare-make build
- run: bare-make install
- run: npm test
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"prettier-config-standard"
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ Fill a buffer with cryptographically secure random bytes, asynchronously providi

The supported hash algorithms.

Constant | Description
--- | ---
`MD5` | A widely-used 128-bit hash function, now considered insecure due to vulnerabilities to collision attacks. Still fast but not recommended for security-sensitive purposes.
`SHA1` | A 160-bit hash function, stronger than MD5 but also broken by collision attacks. Deprecated for most cryptographic uses due to security vulnerabilities.
`SHA256` | Part of the SHA-2 family, this 256-bit hash function is widely used and considered secure for most applications. Slower than MD5 and SHA1 but much more secure.
`SHA512` | Another member of the SHA-2 family, this 512-bit hash function offers greater security than SHA256 but is slower and produces larger hashes. Suitable for high-security environments.
`BLAKE2B256` | A fast, secure alternative to SHA-2 designed for efficiency, producing a 256-bit hash. It is optimized for performance while maintaining strong cryptographic security.
| Constant | Description |
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `MD5` | A widely-used 128-bit hash function, now considered insecure due to vulnerabilities to collision attacks. Still fast but not recommended for security-sensitive purposes. |
| `SHA1` | A 160-bit hash function, stronger than MD5 but also broken by collision attacks. Deprecated for most cryptographic uses due to security vulnerabilities. |
| `SHA256` | Part of the SHA-2 family, this 256-bit hash function is widely used and considered secure for most applications. Slower than MD5 and SHA1 but much more secure. |
| `SHA512` | Another member of the SHA-2 family, this 512-bit hash function offers greater security than SHA256 but is slower and produces larger hashes. Suitable for high-security environments. |
| `BLAKE2B256` | A fast, secure alternative to SHA-2 designed for efficiency, producing a 256-bit hash. It is optimized for performance while maintaining strong cryptographic security. |

## License

Expand Down
67 changes: 67 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Transform, TransformOptions } from 'bare-stream'
import Buffer, { BufferEncoding } from 'bare-buffer'

type Algorithm = 'MD5' | 'SHA1' | 'SHA256' | 'SHA512' | 'BLAKE2B256'

export const constants: { hash: Record<Algorithm, number> }

declare class CryptoError extends Error {
static UNSUPPORTED_DIGEST_METHOD(msg: string): CryptoError
}

export class Hash extends Transform {
constructor(
algorithm: Algorithm | Lowercase<Algorithm> | number,
opts?: TransformOptions<Hash>
)

update(data: string, encoding?: BufferEncoding): this
update(data: Buffer): this

digest(encoding: BufferEncoding): string
digest(): Buffer
}

export function createHash(
algorithm: Algorithm | Lowercase<Algorithm> | number,
opts?: TransformOptions<Hash>
): Hash

export function randomBytes(size: number): Buffer

export function randomBytes(
size: number,
callback: (err: Error | null, buffer: Buffer) => void
): void

export function randomFill<B extends ArrayBuffer | ArrayBufferView>(
buffer: B,
offset?: number,
size?: number
): B

export function randomFill<B extends ArrayBuffer | ArrayBufferView>(
buffer: B,
callback: (err: Error | null, buffer: B) => void
): void

export function randomFill<B extends ArrayBuffer | ArrayBufferView>(
buffer: B,
offset: number,
callback: (err: Error | null, buffer: B) => void
): void

export function randomFill<B extends ArrayBuffer | ArrayBufferView>(
buffer: B,
offset: number,
size: number,
callback: (err: Error | null, buffer: B) => void
): void

export function randomFillSync<B extends ArrayBuffer | ArrayBufferView>(
buffer: B,
offset?: number,
size?: number
): B

export { CryptoError as errors }
Loading

0 comments on commit 7d9631a

Please sign in to comment.