코드 스니펫

실무 코드 스니펫 모음

각 도구별 자주 쓰는 언어/프레임워크 예제입니다. 바로 복사해 적용하세요.

Base64 (Node.js)

텍스트를 Base64로 인코딩/디코딩하는 기본 예제.

// Node.js: Buffer 기반 Base64 인코딩/디코딩
const text = 'hello world'
const encoded = Buffer.from(text, 'utf8').toString('base64')
const decoded = Buffer.from(encoded, 'base64').toString('utf8')

console.log({ encoded, decoded })

JWT 검증 (Node.js)

필수 클레임 검증과 HS256 서명 예제.

// Node.js: 필수 클레임 검증 + HS256 서명
import jwt from 'jsonwebtoken'

const token = jwt.sign(
  { sub: 'user-123', iss: 'dev-tools', aud: 'dev-tools', exp: Math.floor(Date.now() / 1000) + 300 },
  process.env.JWT_SECRET!,
  { algorithm: 'HS256', keyid: 'v1' }
)

jwt.verify(token, process.env.JWT_SECRET!, {
  issuer: 'dev-tools',
  audience: 'dev-tools',
})

CSV → JSON (Python)

BOM 제거와 헤더 검증 포함 변환 예제.

# Python: BOM 제거 + 헤더 검증 + JSON 변환
import csv, json

def convert(path):
    with open(path, 'r', encoding='utf-8-sig', newline='') as f:
        reader = csv.DictReader(f)
        if not reader.fieldnames:
            raise ValueError('Missing header row')
        rows = [row for row in reader]
        return json.dumps(rows, ensure_ascii=False, indent=2)

print(convert('data.csv'))

URL 정규식 검사 (JavaScript)

길이 제한으로 ReDoS를 방지하는 검사 코드.

// JavaScript: URL 유효성 검사 + 길이 제한으로 ReDoS 방지
const urlPattern = /^(https?:\/\/)([\w.-]+)(:[0-9]+)?(\/.*)?$/i

function isSafeUrl(value) {
  if (value.length > 2048) return false
  return urlPattern.test(value)
}

console.log(isSafeUrl('https://example.com'))

SHA-256 해시

민감 정보 해시 생성 시 인코딩/에러 처리를 포함한 예제.

// Node.js: crypto로 SHA-256 해시 생성 (hex)
import crypto from 'node:crypto'

function sha256(value) {
  return crypto.createHash('sha256').update(value, 'utf8').digest('hex')
}

console.log(sha256('secret'))

타임스탬프 변환

Unix 타임 ↔ ISO 문자열 변환과 TZ 주의사항.

// Node.js: Unix 타임 ↔ ISO 변환 (UTC 기준)
const now = Date.now()
const seconds = Math.floor(now / 1000)
const iso = new Date(seconds * 1000).toISOString()
console.log({ seconds, iso })

SQL 파라미터 바인딩

Prepared statement로 SQL 인젝션을 예방하는 예제.

// Node.js: pg로 파라미터 바인딩 (SQL 인젝션 방지)
import { Pool } from 'pg'
const pool = new Pool()

async function findUser(email) {
  const { rows } = await pool.query('SELECT id, email FROM users WHERE email = $1', [email])
  return rows
}
findUser('test@example.com').then(console.log)

Developer Tools

개발자를 위한 18가지 이상의 무료 온라인 도구를 제공합니다. Base64, JSON, JWT, 정규식 등 필수 개발 도구를 한 곳에서.

Developer Tools © 2025. All rights reserved.

모든 도구는 클라이언트 사이드에서만 작동하며, 입력 데이터는 서버로 전송되지 않습니다.