From 3498872467b5492fec93224c2031ce2f5d28e32d Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 24 Jun 2015 14:36:22 -0700 Subject: [PATCH] Add sha256 and sha512 windows compatibility layer --- pkg/utils/crypto/sha256/sha256_darwin.go | 16 ++++++ pkg/utils/crypto/sha256/sha256_windows.go | 47 +++++++++++++++++ pkg/utils/crypto/sha512/sha512_darwin.go | 16 ++++++ pkg/utils/crypto/sha512/sha512_windows.go | 63 +++++++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 pkg/utils/crypto/sha256/sha256_windows.go create mode 100644 pkg/utils/crypto/sha512/sha512_windows.go diff --git a/pkg/utils/crypto/sha256/sha256_darwin.go b/pkg/utils/crypto/sha256/sha256_darwin.go index e688332bb..ddb06ec61 100644 --- a/pkg/utils/crypto/sha256/sha256_darwin.go +++ b/pkg/utils/crypto/sha256/sha256_darwin.go @@ -1,3 +1,19 @@ +/* + * Minimalist Object Storage, (C) 2014 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package sha256 import ( diff --git a/pkg/utils/crypto/sha256/sha256_windows.go b/pkg/utils/crypto/sha256/sha256_windows.go new file mode 100644 index 000000000..ddb06ec61 --- /dev/null +++ b/pkg/utils/crypto/sha256/sha256_windows.go @@ -0,0 +1,47 @@ +/* + * Minimalist Object Storage, (C) 2014 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sha256 + +import ( + "io" + + "crypto/sha256" +) + +// Sum256 - single caller sha256 helper +func Sum256(data []byte) []byte { + d := sha256.New() + d.Write(data) + return d.Sum(nil) +} + +// Sum - io.Reader based streaming sha256 helper +func Sum(reader io.Reader) ([]byte, error) { + d := sha256.New() + var err error + for err == nil { + length := 0 + byteBuffer := make([]byte, 1024*1024) + length, err = reader.Read(byteBuffer) + byteBuffer = byteBuffer[0:length] + d.Write(byteBuffer) + } + if err != io.EOF { + return nil, err + } + return d.Sum(nil), nil +} diff --git a/pkg/utils/crypto/sha512/sha512_darwin.go b/pkg/utils/crypto/sha512/sha512_darwin.go index 433051376..751c7adff 100644 --- a/pkg/utils/crypto/sha512/sha512_darwin.go +++ b/pkg/utils/crypto/sha512/sha512_darwin.go @@ -1,3 +1,19 @@ +/* + * Minimalist Object Storage, (C) 2014 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package sha512 import ( diff --git a/pkg/utils/crypto/sha512/sha512_windows.go b/pkg/utils/crypto/sha512/sha512_windows.go new file mode 100644 index 000000000..751c7adff --- /dev/null +++ b/pkg/utils/crypto/sha512/sha512_windows.go @@ -0,0 +1,63 @@ +/* + * Minimalist Object Storage, (C) 2014 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sha512 + +import ( + "io" + + "crypto/sha512" +) + +// The size of a SHA512 checksum in bytes. +const ( + Size = sha512.Size +) + +// Sum512 - single caller sha512 helper +func Sum512(data []byte) []byte { + d := sha512.New() + d.Write(data) + return d.Sum(nil) +} + +// Sum - io.Reader based streaming sha512 helper +func Sum(reader io.Reader) ([]byte, error) { + d := sha512.New() + var err error + for err == nil { + length := 0 + byteBuffer := make([]byte, 1024*1024) + length, err = reader.Read(byteBuffer) + byteBuffer = byteBuffer[0:length] + d.Write(byteBuffer) + } + if err != io.EOF { + return nil, err + } + return d.Sum(nil), nil +} + +// SumStream - similar to 'Sum()' but returns a [sha512.Size]byte +func SumStream(reader io.Reader) ([sha512.Size]byte, error) { + var returnValue [sha512.Size]byte + sumSlice, err := Sum(reader) + if err != nil { + return returnValue, err + } + copy(returnValue[:], sumSlice) + return returnValue, err +}