Convert Figma logo to code with AI

golang logocrypto

[mirror] Go supplementary cryptography libraries

2,992
1,946
2,992
77

Top Related Projects

25,386

TLS/SSL and crypto library

13,468

Tink is a multi-language, cross-platform, open source library that provides cryptographic APIs that are secure, easy to use correctly, and hard(er) to misuse.

12,174

A modern, portable, easy to use crypto library.

7,179

Stanford Javascript Crypto Library

cryptography is a package designed to expose cryptographic primitives and recipes to Python developers.

Quick Overview

The golang/crypto repository is a collection of supplementary Go cryptography libraries. It extends the standard library's crypto package, providing additional implementations of cryptographic algorithms, protocols, and utilities not included in the core Go distribution.

Pros

  • Offers a wide range of cryptographic primitives and protocols
  • Maintained by the Go team, ensuring high-quality and well-tested code
  • Regularly updated to address security vulnerabilities and improve performance
  • Seamlessly integrates with Go's standard library crypto package

Cons

  • Some implementations may not be as optimized as specialized libraries
  • Documentation can be sparse for certain packages
  • Not all cryptographic algorithms are constantly maintained or updated
  • May require additional security audits for use in highly sensitive applications

Code Examples

  1. Using the PBKDF2 key derivation function:
import "golang.org/x/crypto/pbkdf2"
import "crypto/sha256"

func deriveKey(password, salt []byte) []byte {
    return pbkdf2.Key(password, salt, 4096, 32, sha256.New)
}
  1. Encrypting data using ChaCha20-Poly1305:
import "golang.org/x/crypto/chacha20poly1305"

func encrypt(key, nonce, plaintext, additionalData []byte) ([]byte, error) {
    aead, err := chacha20poly1305.New(key)
    if err != nil {
        return nil, err
    }
    return aead.Seal(nil, nonce, plaintext, additionalData), nil
}
  1. Generating an Ed25519 key pair:
import "golang.org/x/crypto/ed25519"

func generateKeyPair() (ed25519.PublicKey, ed25519.PrivateKey, error) {
    return ed25519.GenerateKey(nil)
}

Getting Started

To use the golang/crypto packages in your Go project:

  1. Install the desired package:

    go get golang.org/x/crypto/[package-name]
    
  2. Import the package in your Go code:

    import "golang.org/x/crypto/[package-name]"
    
  3. Use the imported package's functions and types in your code as shown in the examples above.

Remember to check the documentation for each specific package you're using, as usage may vary between different cryptographic primitives and protocols.

Competitor Comparisons

25,386

TLS/SSL and crypto library

Pros of OpenSSL

  • Comprehensive cryptographic library with a wide range of algorithms and protocols
  • Extensively used and battle-tested in production environments
  • Supports hardware acceleration for improved performance

Cons of OpenSSL

  • Complex API and steep learning curve
  • Written in C, which can be more prone to security vulnerabilities
  • Larger codebase and more dependencies

Code Comparison

OpenSSL (C):

EVP_CIPHER_CTX *ctx;
EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv);
EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len);
EVP_EncryptFinal_ex(ctx, ciphertext + len, &len);

crypto (Go):

block, _ := aes.NewCipher(key)
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext, plaintext)

Summary

OpenSSL is a more comprehensive and widely-used cryptographic library, offering a broader range of features and hardware acceleration. However, it has a steeper learning curve and potential security risks due to its C implementation. The crypto package in Go provides a simpler, more idiomatic API for Go developers, with a focus on safety and ease of use. While it may not offer as many features as OpenSSL, it is well-suited for Go projects requiring cryptographic operations.

13,468

Tink is a multi-language, cross-platform, open source library that provides cryptographic APIs that are secure, easy to use correctly, and hard(er) to misuse.

Pros of Tink

  • Provides a higher-level, more user-friendly API for cryptographic operations
  • Offers built-in key management and rotation features
  • Supports a wider range of cryptographic primitives and algorithms

Cons of Tink

  • Less flexible for low-level cryptographic operations
  • May have a steeper learning curve for developers familiar with Go's standard crypto package
  • Potentially larger dependency footprint in projects

Code Comparison

Tink example (encryption):

keysetHandle, err := keyset.NewHandle(aead.AES256GCMKeyTemplate())
a, err := aead.New(keysetHandle)
ciphertext, err := a.Encrypt(plaintext, associatedData)

Crypto example (encryption):

block, err := aes.NewCipher(key)
aesgcm, err := cipher.NewGCM(block)
ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil)

Summary

Tink offers a more comprehensive and user-friendly approach to cryptography, with built-in key management and a wider range of supported algorithms. However, it may be less suitable for projects requiring fine-grained control over cryptographic operations. Crypto provides a lower-level interface, offering more flexibility but requiring more expertise to use correctly. The choice between the two depends on the specific needs of the project and the development team's expertise.

12,174

A modern, portable, easy to use crypto library.

Pros of libsodium

  • Cross-platform support with bindings for multiple languages
  • Extensive documentation and easier API for cryptographic operations
  • Audited and battle-tested implementation of modern cryptographic primitives

Cons of libsodium

  • Requires external dependency and linking, unlike Go's built-in crypto package
  • May have slightly lower performance in some cases due to its C implementation
  • Less idiomatic for Go developers compared to the native crypto package

Code Comparison

libsodium (C):

unsigned char key[crypto_secretbox_KEYBYTES];
unsigned char nonce[crypto_secretbox_NONCEBYTES];
unsigned char ciphertext[MESSAGE_LEN + crypto_secretbox_MACBYTES];

crypto_secretbox_easy(ciphertext, message, MESSAGE_LEN, nonce, key);

crypto (Go):

block, _ := aes.NewCipher(key)
aesgcm, _ := cipher.NewGCM(block)
ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil)

Both libraries provide cryptographic functions, but libsodium offers a more straightforward API with built-in nonce and key generation. The crypto package in Go requires more manual setup but integrates seamlessly with Go's standard library and ecosystem.

7,179

Stanford Javascript Crypto Library

Pros of sjcl

  • Written in JavaScript, making it easily integrable into web applications
  • Provides a simple, high-level API for common cryptographic operations
  • Includes a password-based key derivation function (PBKDF2) out of the box

Cons of sjcl

  • Limited to JavaScript ecosystem, less versatile than Go-based crypto
  • May have performance limitations compared to compiled languages like Go
  • Smaller community and potentially less frequent updates

Code Comparison

sjcl:

var encrypted = sjcl.encrypt("password", "data");
var decrypted = sjcl.decrypt("password", encrypted);

crypto:

ciphertext, err := aes.Encrypt(key, plaintext)
plaintext, err := aes.Decrypt(key, ciphertext)

Key Differences

  • sjcl focuses on browser-based cryptography, while crypto is a general-purpose cryptographic library for Go
  • crypto offers lower-level primitives, giving developers more control over implementation details
  • sjcl provides a more abstracted API, potentially sacrificing flexibility for ease of use

Use Cases

  • sjcl: Web applications requiring client-side encryption
  • crypto: Server-side applications, command-line tools, and Go-based projects needing cryptographic functions

Community and Support

  • crypto has a larger community due to its association with the Go programming language
  • sjcl has a smaller but dedicated community, primarily focused on web-based cryptography

cryptography is a package designed to expose cryptographic primitives and recipes to Python developers.

Pros of cryptography

  • More comprehensive and feature-rich library for cryptographic operations
  • Extensive documentation and user-friendly API
  • Active community support and regular updates

Cons of cryptography

  • Slower performance compared to Go's native implementation
  • Requires additional dependencies and setup

Code Comparison

cryptography (Python):

from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
token = f.encrypt(b"Secret message")

crypto (Go):

import "crypto/aes"
key := []byte("example key 1234")
block, _ := aes.NewCipher(key)
ciphertext := make([]byte, aes.BlockSize+len(plaintext))

Summary

cryptography offers a more comprehensive and user-friendly approach to cryptographic operations in Python, with extensive documentation and community support. However, it may have slower performance compared to crypto, which provides native Go implementations. crypto is more lightweight but may require more manual implementation of certain features. The choice between the two depends on the specific language requirements and performance needs of the project.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Go Cryptography

Go Reference

This repository holds supplementary Go cryptography libraries.

Download/Install

The easiest way to install is to run go get -u golang.org/x/crypto/.... You can also manually git clone the repository to $GOPATH/src/golang.org/x/crypto.

Report Issues / Send Patches

This repository uses Gerrit for code changes. To learn how to submit changes to this repository, see https://golang.org/doc/contribute.html.

The main issue tracker for the crypto repository is located at https://github.com/golang/go/issues. Prefix your issue with "x/crypto:" in the subject line, so it is easy to find.

Note that contributions to the cryptography package receive additional scrutiny due to their sensitive nature. Patches may take longer than normal to receive feedback.