[go] go 언어에서 지원하는 암호화 알고리즘

Go 언어는 다양한 암호화 알고리즘을 지원하며, 데이터 보호 및 보안 기능을 구현하는 데 사용될 수 있습니다. 이 포스트에서는 Go 언어에서 지원하는 일반적인 암호화 알고리즘에 대해 살펴보겠습니다.

목차

  1. 해시 함수
  2. 대칭키 암호화
  3. 비대칭키 암호화
  4. 인증 및 디지털 서명

해시 함수

해시 함수는 고정된 크기의 데이터를 다른 크기로 변환하는 암호화 알고리즘입니다. Go 언어에서는 MD5, SHA-1, SHA-256, SHA-512 등의 해시 함수를 제공합니다.

package main

import (
	"crypto/md5"
	"crypto/sha1"
	"crypto/sha256"
	"crypto/sha512"
	"fmt"
)

func main() {
	data := []byte("Hello, World!")
	fmt.Printf("MD5: %x\n", md5.Sum(data))
	fmt.Printf("SHA-1: %x\n", sha1.Sum(data))
	fmt.Printf("SHA-256: %x\n", sha256.Sum256(data))
	fmt.Printf("SHA-512: %x\n", sha512.Sum512(data))
}

대칭키 암호화

대칭키 암호화는 동일한 키를 사용하여 암호화 및 복호화하는 알고리즘입니다. Go 언어에서는 AES, DES, TDES 등의 대칭키 암호화를 지원합니다.

package main

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"fmt"
	"io"
)

func main() {
	key := []byte("verysecretkey123")
	plaintext := []byte("Hello, World!")

	block, _ := aes.NewCipher(key)
	ciphertext := make([]byte, aes.BlockSize+len(plaintext))
	iv := ciphertext[:aes.BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		panic(err)
	}
	stream := cipher.NewCFBEncrypter(block, iv)
	stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
	fmt.Printf("%x\n", ciphertext)
}

비대칭키 암호화

비대칭키 암호화는 공개키와 개인키를 사용하여 암호화 및 복호화하는 알고리즘입니다. Go 언어에서는 RSA, ECDSA 등의 비대칭키 암호화를 지원합니다.

package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"fmt"
)

func main() {
	privateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
	publicKey := &privateKey.PublicKey

	message := []byte("Hello, World!")

	ciphertext, _ := rsa.EncryptPKCS1v15(rand.Reader, publicKey, message)
	fmt.Printf("%x\n", ciphertext)

	plaintext, _ := rsa.DecryptPKCS1v15(rand.Reader, privateKey, ciphertext)
	fmt.Printf("%s\n", plaintext)
}

인증 및 디지털 서명

Go 언어는 인증 및 디지털 서명을 위한 기능도 제공합니다. 대표적으로 RSA, ECDSA 알고리즘을 사용하여 디지털 서명을 생성하고 검증할 수 있습니다.

package main

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"crypto/sha256"
	"fmt"
)

func main() {
	privateKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
	publicKey := &privateKey.PublicKey

	message := []byte("Hello, World!")

	hash := sha256.Sum256(message)
	r, s, _ := ecdsa.Sign(rand.Reader, privateKey, hash[:])
	signature := append(r.Bytes(), s.Bytes()...)

	verified := ecdsa.Verify(publicKey, hash[:], r, s)
	fmt.Printf("Signature verified: %v\n", verified)
}

Go 언어는 다양한 암호화 알고리즘을 통해 데이터 보안 및 보호를 지원하여 안전한 응용 프로그램을 개발할 수 있도록 도와줍니다. 암호화에 대한 더 자세한 정보는 Go 공식 문서를 참고하십시오.

참고자료