Run Format

Source file src/pkg/crypto/crypto.go

     1	// Copyright 2011 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// Package crypto collects common cryptographic constants.
     6	package crypto
     7	
     8	import (
     9		"hash"
    10		"strconv"
    11	)
    12	
    13	// Hash identifies a cryptographic hash function that is implemented in another
    14	// package.
    15	type Hash uint
    16	
    17	const (
    18		MD4       Hash = 1 + iota // import code.google.com/p/go.crypto/md4
    19		MD5                       // import crypto/md5
    20		SHA1                      // import crypto/sha1
    21		SHA224                    // import crypto/sha256
    22		SHA256                    // import crypto/sha256
    23		SHA384                    // import crypto/sha512
    24		SHA512                    // import crypto/sha512
    25		MD5SHA1                   // no implementation; MD5+SHA1 used for TLS RSA
    26		RIPEMD160                 // import code.google.com/p/go.crypto/ripemd160
    27		maxHash
    28	)
    29	
    30	var digestSizes = []uint8{
    31		MD4:       16,
    32		MD5:       16,
    33		SHA1:      20,
    34		SHA224:    28,
    35		SHA256:    32,
    36		SHA384:    48,
    37		SHA512:    64,
    38		MD5SHA1:   36,
    39		RIPEMD160: 20,
    40	}
    41	
    42	// Size returns the length, in bytes, of a digest resulting from the given hash
    43	// function. It doesn't require that the hash function in question be linked
    44	// into the program.
    45	func (h Hash) Size() int {
    46		if h > 0 && h < maxHash {
    47			return int(digestSizes[h])
    48		}
    49		panic("crypto: Size of unknown hash function")
    50	}
    51	
    52	var hashes = make([]func() hash.Hash, maxHash)
    53	
    54	// New returns a new hash.Hash calculating the given hash function. New panics
    55	// if the hash function is not linked into the binary.
    56	func (h Hash) New() hash.Hash {
    57		if h > 0 && h < maxHash {
    58			f := hashes[h]
    59			if f != nil {
    60				return f()
    61			}
    62		}
    63		panic("crypto: requested hash function #" + strconv.Itoa(int(h)) + " is unavailable")
    64	}
    65	
    66	// Available reports whether the given hash function is linked into the binary.
    67	func (h Hash) Available() bool {
    68		return h < maxHash && hashes[h] != nil
    69	}
    70	
    71	// RegisterHash registers a function that returns a new instance of the given
    72	// hash function. This is intended to be called from the init function in
    73	// packages that implement hash functions.
    74	func RegisterHash(h Hash, f func() hash.Hash) {
    75		if h >= maxHash {
    76			panic("crypto: RegisterHash of unknown hash function")
    77		}
    78		hashes[h] = f
    79	}
    80	
    81	// PublicKey represents a public key using an unspecified algorithm.
    82	type PublicKey interface{}
    83	
    84	// PrivateKey represents a private key using an unspecified algorithm.
    85	type PrivateKey interface{}

View as plain text