github.com/lzy4123/fabric@v2.1.1+incompatible/bccsp/sw/aeskey.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package sw 17 18 import ( 19 "crypto/sha256" 20 "errors" 21 22 "github.com/hyperledger/fabric/bccsp" 23 ) 24 25 type aesPrivateKey struct { 26 privKey []byte 27 exportable bool 28 } 29 30 // Bytes converts this key to its byte representation, 31 // if this operation is allowed. 32 func (k *aesPrivateKey) Bytes() (raw []byte, err error) { 33 if k.exportable { 34 return k.privKey, nil 35 } 36 37 return nil, errors.New("Not supported.") 38 } 39 40 // SKI returns the subject key identifier of this key. 41 func (k *aesPrivateKey) SKI() (ski []byte) { 42 hash := sha256.New() 43 hash.Write([]byte{0x01}) 44 hash.Write(k.privKey) 45 return hash.Sum(nil) 46 } 47 48 // Symmetric returns true if this key is a symmetric key, 49 // false if this key is asymmetric 50 func (k *aesPrivateKey) Symmetric() bool { 51 return true 52 } 53 54 // Private returns true if this key is a private key, 55 // false otherwise. 56 func (k *aesPrivateKey) Private() bool { 57 return true 58 } 59 60 // PublicKey returns the corresponding public key part of an asymmetric public/private key pair. 61 // This method returns an error in symmetric key schemes. 62 func (k *aesPrivateKey) PublicKey() (bccsp.Key, error) { 63 return nil, errors.New("Cannot call this method on a symmetric key.") 64 }